SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SELECT setter


SELECT setter

Author
Message
stoklosa
stoklosa
SSC Veteran
SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)SSC Veteran (287 reputation)

Group: General Forum Members
Points: 287 Visits: 21
Comments posted to this topic are about the item SELECT setter
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9031 Visits: 7283
Usig SELECT is a versatile method of allocating values to multiple variables as well.
good "back to basics" question.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Fal
Fal
Mr or Mrs. 500
Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)Mr or Mrs. 500 (568 reputation)

Group: General Forum Members
Points: 568 Visits: 1803
A good question to remind us of something we all might forget: WHERE isn't just used after FROM.

S.
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32875 Visits: 18559
Agreed. Good back to basics question.


Thanks



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Open Minded
Open Minded
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 567
I got the answer right because I was looking at the logic, 'where 1=0'.

But when I changed it to 'where 1=1', the output came out as 0.

If a 'A SELECT statement that doesn't return results doesn't set a variable to anything.', then why didn't it remain at 1 when I changed the logic to 'where 1=1' ?

I hope I don't sound terrible naive about this. Below is the code I used to check the veracity of the statement:

DECLARE @i INT

SELECT @i = 1
SELECT @i = 0 where 1=0
SELECT @i as i

SELECT @i = 1
SELECT @i = 0 where 1=1
SELECT @i as i
SELECT @i

May I ask for further enlightenment? Thank you.
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9031 Visits: 7283
If a 'A SELECT statement that doesn't return results doesn't set a variable to anything.', then why didn't it remain at 1 when I changed the logic to 'where 1=1' ?


if you run the following statements:
select 'Has no value' where 1 = 0
select 'Has value' where 1 = 1



you will see that the first statement does not return any rows, but the second does.
in this vein, the question posted has "where 1 = 0", which does not generate a result set, which will not change the variable's value.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Open Minded
Open Minded
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 567
Thanks! I hope I got this right: if the WHERE clause will create a result or boolean TRUE (1), then the SELECT part makes a change, if the SELECT part happens to be changing the value of a variable.

It will be just like reversing an IF-THEN statement, like THEN-IF. Well, I hope I got that analogy right also.
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCrazy Eights
SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)SSCrazy Eights (9K reputation)

Group: General Forum Members
Points: 9031 Visits: 7283
That is correct.
Wherever the predicate equates to true, an action will take place.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Steve Jones
Steve Jones
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: Administrators
Points: 63901 Visits: 19116
I suspected no assignment, but I had these visions of C where = means something happens.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
john.arnott
john.arnott
SSCrazy
SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)SSCrazy (2K reputation)

Group: General Forum Members
Points: 2012 Visits: 3059
I'm proud of my wrong answer ("NULL") as it shows I thought it through, even if incorrectly, before triying the script in SSMS. <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>

Meanwhile, for those interested in seeing this behavior in SSMS....

DECLARE @i INT

SELECT @i = 0
SELECT @i AS i --Returns 0
SELECT @i = 1 where 1=0 --Null; assignment ignored
SELECT @i AS i --Returns 0
SELECT @i = (SELECT 2 where 1=0)--Assign result of subSelect with FALSE condition ***
SELECT @i AS i --Returns NULL
SELECT @i = 3 where 1=1 --Condition is true, so assignment works
SELECT @i AS i --Returns 3
SELECT @i = (SELECT 4 where 1=1)--Assign result of subSelect with TRUE condition ***
SELECT @i AS i --Returns 4


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search