|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Saturday, September 29, 2012 6:38 AM
Points: 276,
Visits: 21
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 8:31 AM
Points: 3,129,
Visits: 4,312
|
|
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”
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, April 03, 2013 10:05 PM
Points: 584,
Visits: 1,571
|
|
A good question to remind us of something we all might forget: WHERE isn't just used after FROM.
S.
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 18,754,
Visits: 12,337
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, April 11, 2013 12:58 AM
Points: 174,
Visits: 555
|
|
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.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 8:31 AM
Points: 3,129,
Visits: 4,312
|
|
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”
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, April 11, 2013 12:58 AM
Points: 174,
Visits: 555
|
|
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.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: 2 days ago @ 8:31 AM
Points: 3,129,
Visits: 4,312
|
|
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”
|
|
|
|
|
SSC-Dedicated
           
Group: Administrators
Last Login: Today @ 11:20 AM
Points: 31,437,
Visits: 13,752
|
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
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
|
|
|
|