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


Assigning values to Variables with CASE


Assigning values to Variables with CASE

Author
Message
Pulivarthi Sasidhar
Pulivarthi Sasidhar
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2464 Visits: 1026
Comments posted to this topic are about the item Assigning values to Variables with CASE
Danny Ocean
Danny Ocean
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2232 Visits: 1549
Good example. But rather than creating six different variables, i would like to create only one variable and use sp_executesql.

Anyway. Thanks for question.

Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
www.GrowWithSql.com
palotaiarpad
palotaiarpad
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2432 Visits: 804
Jesus, i need my coffee Wink (Failed)
Toreador
Toreador
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3847 Visits: 8125
An object lesson in the need for well-commented code!
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17287 Visits: 7418
Interesting question thanks.

____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
call.copse
call.copse
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5657 Visits: 2010
Toreador (4/3/2013)
An object lesson in the need for well-commented code!


+1

Just a little help with the intention of some of the string manipulation might make the point clearer.
call.copse
call.copse
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5657 Visits: 2010
Toreador (4/3/2013)
An object lesson in the need for well-commented code!


+1

Just a little help with the intention of some of the string manipulation might make the point clearer.
demonfox
demonfox
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2185 Visits: 1192
The case statement will assign 'FF' to @str6 ,Remaining varaibles contains NULL values by default and not assigned with corresponding values of AA,BB,CC,DD and EE. The values are assigned to variables after completion loop.


It looked like variables overwrite to me... and I didn't see any reference suggesting otherwise.

At the start of the loop , @str1 is assigned a value AA , and then with the next cycle gets assigned to NULL . At the end of the loop all ,@STR* , becomes NULL but @str6; it is FF and didn't get to see the next loop.

In the next loop, it gets interesting, as the NULLIF and ISNULL comes into the use; the result output becomes constant for string @cmpstr ,i.e. FF until any number of loop execution.

~ demonfox
___________________________________________________________________
Wondering what I would do next , when I am done with this one Ermm
Koen Verbeeck
Koen Verbeeck
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: General Forum Members
Points: 63458 Visits: 13298
Explanation could have been a little bit better.
And question a little bit shorter :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18949 Visits: 12426
I like the idea of the question - testing when an assignment to a variable with CASE would or would not overwrite previous assignments. I can see some people getting that wrong.

Unfortunately, the execution of the idea left to be desired. The code was long and complicated, making this question more an exercise in parsing code. The exact same question idea could have been done with a much shorter code sample:
DECLARE @Str1 varchar(10) = '',
@Str2 varchar(10) = '',
@Val int;

SET @Val = 1;
SELECT @Str1 = CASE WHEN @Val = 1 THEN 'One' ELSE NULL END,
@Str2 = CASE WHEN @Val = 2 THEN 'Two' ELSE NULL END;
SET @Val = 2;
SELECT @Str1 = CASE WHEN @Val = 1 THEN 'One' ELSE NULL END,
@Str2 = CASE WHEN @Val = 2 THEN 'Two' ELSE NULL END;

SELECT @Str1, @Str2;




Sasidhar Pulivarthi, you obviously have good ideas for questions. I hope you will continue to submit question, and that you will take this feedback to try and convert your good ideas into even better questions going forward.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
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