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 8540043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:30:38+00:00 2026-06-11T11:30:38+00:00

Everyday I learn something new, it seems :) Can someone please explain to me

  • 0

Everyday I learn something new, it seems 🙂 Can someone please explain to me the rationale behind the following code behavior:

DECLARE @A INT

SET @A = 15
SET @A = (SELECT ValueThatDoesntExist FROM dbo.MyTable WHERE MyColumn = 'notfound')

SELECT @A
-- Rsultset is NULL

SET @A = 15
SELECT @A = ValueThatDoesntExist FROM dbo.MyTable WHERE MyColumn = 'notfound'

SELECT @A
-- Resultset is 15

From what I see, SET changes the value of the variable if the resultset is NULL, while SELECT doesn’t. Is this normal ANSI behavior or is it T-SQL specific?

Of, course, if I do SELECT @A = NULL, the assignment happens correctly.

  • 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-11T11:30:39+00:00Added an answer on June 11, 2026 at 11:30 am

    The first version sets A to the result of a query:

    SET @A = (SELECT ValueThatDoesntExist FROM dbo.MyTable WHERE MyColumn = 'notfound')
    

    Basically the select in in scalar context, and if it doesn’t find a row, it evaluates to null.

    The second version sets A for each row in the result set:

    SELECT @A = ValueThatDoesntExist FROM dbo.MyTable WHERE MyColumn = 'notfound'
    

    Since there are no rows, A is never assigned to. Another example:

    declare @a int
    
    select  @a = i
    from    (
            select  1
            union all select 2
            union all select 3
            ) as SubQueryAlias(i)
    order by
            i
    
    select  @a
    

    This will assign 3 values to @a. The last one assigned is 3, so that’s what the query prints.

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

Sidebar

Related Questions

I learn something new everyday about C# and came across this construct. I am
I try to devote certain time everyday to learn new skills while also improve
For an everyday-user with new hardware Linux seems for me the natural choice if
We learn everyday. I came across this script and though i am new to
I want to display multi-buttons alertview to user everyday twice a day. They can
Everyday is a new day with Symfony, but I'm loving it! This morning I
I code with Vb6 everyday using my own convention and i'm starting to feel
I want to learn a functional language and Mnesia seems killer app enough for
I want to execute a job everyday 2PM. Which method of java.util.Timer I can
What naming conventions do you use for everyday code? I'm pondering this because I

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.