I am a beginner trying to learn T-SQL and here is where I was stuck. Can anyone please help me out with this.
use AdventureWorks go
declare @n as int =1
n = CASE
when n=1 then print('n=1')
when n=2 then print ('n=2')
print ('that implies n>1')
when n=2+2 then print('n=4')
print('n is some other value.')
Thankyou very much.....It would be of great help !!
asked May 21, 2010 at 06:24 PM in Default
you should be using IF . . ELSE for flow control not CASE .. WHEN
CASE .. WHEN is to use within a QUERY
answered May 21, 2010 at 08:59 PM
You can't mix assignment with output in CASE statement.
You can print the output basing on the value of the @n variable using following syntax for example:
Please note that each time you use variable it must be prefixed with @ and print statement contains conditional expression that determines what has to be printed. Alternatively, you can assign the output of case statement to a variable and print it:
When you assign a value to a variable, you have to use keyword
answered May 21, 2010 at 10:05 PM