Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Here is a sample codeIF COLOR = 'BLUE'BEGINSET COLOR = 'RED'ENDIF COLOR = 'RED'BEGINSET COLOR = 'YELLOW'ENDSo if I pass a value that is blue, will the result be RED or YELLOW? I am trying to understand a script I inherited where I only want COLOR to change based on the ORIGINAL. So if it's BLUE, I want it to become RED, not YELLOW.
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2012-08-23 : 13:51:37
You would need ELSE as in:
IF @COLOR = 'BLUE'BEGIN SET @COLOR = 'RED'ENDELSE IF @COLOR = 'RED'BEGIN SET @COLOR = 'YELLOW'END
An alternate way that I like better is using a CASE expression:
SET @COLOR = CASE WHEN @color = 'BLUE' THEN 'RED' WHEN @color = 'RED' THEN 'YELLOW' ELSE @colorEND
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2012-08-23 : 14:10:25
you can also use simple case structure hereCASE @ColorWHEN 'BLUE' THEN 'RED'WHEN 'RED' THEN 'YELLOW'ELSE @ColorEND------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/