Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:17:12+00:00 2026-06-03T00:17:12+00:00

My code is as follows: SET @var = (SELECT var FROM dbo.varDB WHERE varName

  • 0

My code is as follows:

SET @var = (SELECT var FROM dbo.varDB WHERE varName = @varName)

IF (@@ROWCOUNT > 0)
    BEGIN
        //carry out insert
    END

Am I right in saying this will always return a 1 for @@ROWCOUNT as it carries out an assignment last?

If so, what would be the most elegant solution to allow me to:

  • Create variable with the result from my query stored in it
  • Test if the query returns any results
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:17:14+00:00Added an answer on June 3, 2026 at 12:17 am

    The reason it returns one is because you’ve assigned exactly one value (some arbitrary value). You should be using EXISTS instead of doing a manual count anyway, as @Sive suggests. The reason is that EXISTS can perform no worse than COUNT, and in the use case you’re describing, it doesn’t really matter what the actual count is anyway. You want to know if the count is zero or greater than zero.

    The problem with assigning that value to a variable in the way you’ve done it, is what happens if there are multiple rows that match? Let’s try it:

    DECLARE @foo TABLE([var] INT, varname SYSNAME);
    INSERT @foo VALUES (1,N'bob'),(2,N'bob');
    DECLARE @var INT;
    SET @var = (SELECT [var] FROM @foo WHERE varname = N'bob
    

    Result:

    Msg 512, Level 16, State 1, Line 4
    Subquery returned more than 1
    value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

    Now, if varname is unique, I would do this:

    SELECT @var = [var] FROM dbo.varDB WHERE varName = @varName;
    IF @var IS NOT NULL
    BEGIN
        // carry out insert
    END
    ELSE
    BEGIN
        PRINT 'Already existed! ' + RTRIM(@var);
    END
    

    If varname is not unique, then I’m not sure what you’re going to do with a single value. Which one did it pull? Imagine this scenario:

    DECLARE @foo TABLE([var] INT, varname SYSNAME);
    INSERT @foo VALUES (3,N'bob'),(2,N'adam'),(1,N'bob');
    DECLARE @var INT;
    SELECT @var = [var] FROM @foo WHERE varname = N'bob';
    PRINT @var;
    

    Will @var be 1 or 3? Who knows? Why does it matter which one it is if you’re only capturing one of potentially many values? Are you going to do something with that row but not the others?

    In addition, if your intention is to insert data from this table, why not simply throw out the up-front check, the row count etc. and just say:

    INSERT dbo.SomeTable(column1 --,...other columns
      SELECT var --,...other columns
      FROM dbo.varDB
      WHERE varName = @varName;
    

    The insert won’t happen if the row doesn’t exist.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using silverlight, My code is set up for a usercontrol as follows:
I have code as follows: $(#item_select).change(function() { var params = $(#item_select option:selected).val(); $.post('/account/ar_form.php', {idata:
I have a piece of javascript code as follows: var data = { ...
I've got code as follows: var smtpClient = new SmtpClient(my.mail.server); smtpClient.Send(mailmessage); however the send
I have a sample CSV file as follows 1,A 2,B 3,C Code: var query
Why can't I do something like this in c# (pseudo code follows) Interface1 {
I'm using this plugin: http://www.jeremymartin.name/projects.php?project=kwicks And my code follows this example: http://www.jeremymartin.name/examples/kwicks.php?example=7 I'm using
For some code as follows, opts, args = getopt.getopt(sys.argv[1:], c:, ... for o,v in
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I have 3 different jquery uses & put it into one code as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.