|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Tuesday, October 11, 2011 1:51 PM
Points: 341,
Visits: 311
|
|
| GAH! I don't really know what I was thinking when I answered. Obvious answer, but I obviously got it wrong. *grumble*
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 5:28 AM
Points: 2,659,
Visits: 720
|
|
I got it right because I knew it, but I understand why that might not be obvious, and the behaviour is slightly iffy.
If you do
SELECT @var = MAX(object_id) FROM sys.objects WHERE 1=0 you'll get NULL assigned, but
SELECT @var = object_id FROM sys.objects WHERE 1=0 will retain the previous value of @var.
It's really a source for unexpected behaviour when a WHERE-clause is a little wrong, and relying on it when coding isn't really fair to whoever will maintain your code.
Just because you're right doesn't mean everybody else is wrong.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: 2 days ago @ 9:31 AM
Points: 1,040,
Visits: 1,356
|
|
john.arnott (2/11/2010)
<rant on> I can't imagine that 85% of respondants actualy knew the result of the SELECT where the condition is false would not be assigned to the variable. </rant>
What difference does it make? At the end of the day, answering a good QotD correctly only gives you two things: 1. A better understanding of SQL / SQL Server. 2. An increase to your SSC points.
The better understanding of SQL / SQL Server comes when you either think the question through or research it out. It does not come from simply copying and pasting into SSMS -- that only tells you what happens, not how or why.
The SSC points are cute, and can be a fun little game to play with your fellow SSCers, but they have no intrinsic value beyond this site.
So, those people who just do the copy-and-paste are actually cheating themselves out of better knowledge for a few measley points. Let them.
For the record, I got this one correct because I've seen this before.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
sknox,
What difference does it make? At the end of the day, answering a good QotD correctly only gives you two things: 1. A better understanding of SQL / SQL Server. 2. An increase to your SSC points. . . . So, those people who just do the copy-and-paste are actually cheating themselves out of better knowledge for a few measley points. Let them.
Exactly. The difference is that it's frustrating to think that people are cheating themselves for the sake of the stupid points. I put my comment inside pseudo-tags as an indication of what I meant to be wry sarcasm. I think we agree on the basic issue. I would hope that the points-hawks read your post and take it to heart.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:02 PM
Points: 7,102,
Visits: 7,165
|
|
Rune Bivrin (2/12/2010)
I got it right because I knew it, but I understand why that might not be obvious, and the behaviour is slightly iffy. If you do SELECT @var = MAX(object_id) FROM sys.objects WHERE 1=0 you'll get NULL assigned, but SELECT @var = object_id FROM sys.objects WHERE 1=0 will retain the previous value of @var. It's really a source for unexpected behaviour when a WHERE-clause is a little wrong, and relying on it when coding isn't really fair to whoever will maintain your code. It isn't really iffy, and it's the behaviour which you would naturally expect if you looked at the select - you just have to consider what the raw select (without the local variable assignment) would return.
SELECT max(object_id) from sys.objects where 1=0 returns NULL because applying max to an empty set produces NULL. So SELECT @var=max(object_id) from sys.objects where 1=0 sets @var to NULL. On the other hand, SELECT object_id from sys.objects where 1=0 doesn't return anything, so SELECT @var = object_id from sys.objects where 1=0 doesn't have any value (not even null) to assign to @var and so it doesn't assign anything.
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 5:28 AM
Points: 2,659,
Visits: 720
|
|
Oh, I know that. From the perspective of someone who started with SQL Server way back in 91 it's completely clear. But for the less experienced it can certainly appear strange and unexpected.
Just because you're right doesn't mean everybody else is wrong.
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Today @ 1:02 AM
Points: 10,989,
Visits: 10,534
|
|
This difficult-to-recall behaviour is the main reason I prefer SET @x = (SELECT...) syntax.
Paul White SQL Server MVP SQLblog.com @SQL_Kiwi
|
|
|
|