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


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 6:39 AM
Points: 4,158, Visits: 5,553
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


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Sunday, November 2, 2014 10:02 PM
Points: 506, Visits: 1,695
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


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 7:07 AM
Points: 18,060, Visits: 16,094
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
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: Monday, August 18, 2014 4:04 AM
Points: 176, 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.
Post #863858
Posted Thursday, February 11, 2010 12:02 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 6:39 AM
Points: 4,158, Visits: 5,553
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: Monday, August 18, 2014 4:04 AM
Points: 176, 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.
Post #863868
Posted Thursday, February 11, 2010 1:40 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 6:39 AM
Points: 4,158, Visits: 5,553
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: Yesterday @ 10:36 AM
Points: 31,362, Visits: 15,823
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


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,388, Visits: 3,039
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