Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

SELECT setter Expand / Collapse
Author
Message
Posted Wednesday, February 10, 2010 9:17 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Saturday, September 29, 2012 6:38 AM
Points: 276, Visits: 21
Comments posted to this topic are about the item SELECT setter
Post #863801
Posted Wednesday, February 10, 2010 10:25 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:52 AM
Points: 3,866, Visits: 5,017
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”
Post #863830
Posted Wednesday, February 10, 2010 11:07 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Thursday, July 3, 2014 9:19 PM
Points: 605, Visits: 1,691
A good question to remind us of something we all might forget: WHERE isn't just used after FROM.

S.
Post #863848
Posted Wednesday, February 10, 2010 11:18 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:47 PM
Points: 21,340, Visits: 15,016
Agreed. Good back to basics question.


Thanks




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


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #863852
Posted Wednesday, February 10, 2010 11:46 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 6, 2014 12:28 AM
Points: 176, Visits: 565
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.
Post #863858
Posted Thursday, February 11, 2010 12:02 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:52 AM
Points: 3,866, Visits: 5,017
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”
Post #863863
Posted Thursday, February 11, 2010 12:12 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 6, 2014 12:28 AM
Points: 176, Visits: 565
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.
Post #863868
Posted Thursday, February 11, 2010 1:40 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 7:52 AM
Points: 3,866, Visits: 5,017
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”
Post #863908
Posted Thursday, February 11, 2010 10:30 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 5:13 PM
Points: 33,100, Visits: 15,210
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
Post #864180
Posted Thursday, February 11, 2010 11:16 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 6:26 AM
Points: 1,521, Visits: 3,036
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

Post #864201
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse