May 19, 2014 at 3:20 pm
Hi,
I am having a hard time in making case expression work in T-SQL. Please see below script. I have created a sample script and testing it, but it is not working.
create proc dbo.proc1 @input int
as
case @input
when 1 then 'yes'
else 'No'
end;
When i tried to create the proc above, i get "Incorrect syntax near the word case".
But if i use the case in statement it works like .. select case col1 when 1 then 'yes' from table; This statement works.
Not sure why the expresssion are not working in T-SQL.
I appreciate your responses.
May 19, 2014 at 3:23 pm
vasuarjula (5/19/2014)
Hi,I am having a hard time in making case expression work in T-SQL. Please see below script. I have created a sample script and testing it, but it is not working.
create proc dbo.proc1 @input int
as
case @input
when 1 then 'yes'
else 'No'
end;
When i tried to create the proc above, i get "Incorrect syntax near the word case".
But if i use the case in statement it works like .. select case col1 when 1 then 'yes' from table; This statement works.
Not sure why the expresssion are not working in T-SQL.
I appreciate your responses.
CASE is an expression in sql. That means it is NOT a statement. It MUST be part of a statement.
http://msdn.microsoft.com/en-us/library/ms181765.aspx
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
May 19, 2014 at 3:38 pm
create proc dbo.proc1 @input int
as
select case @input
when 1 then 'yes'
else 'No'
end;
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply