Happy
Happy

Reputation: 1145

how to use case sql server 2008

i need assign value word by numeric so i tried below code.but its showing error"Incorrect syntax near the keyword 'case'."

declare @ww int

set @ww=1
case @ww
WHEN  '1' THEN  print='one'

ELSE NULL 

where i made error...?

Upvotes: 0

Views: 68

Answers (3)

Craig Moore
Craig Moore

Reputation: 1091

Please read http://technet.microsoft.com/en-us/library/ms181765.aspx

In there it states "CASE can be used in any statement or clause that allows a valid expression. For example, you can use CASE in statements such as SELECT, UPDATE, DELETE and SET, and in clauses such as select_list, IN, WHERE, ORDER BY, and HAVING. ". This is not the case here.

In response to your comment (as you cannot line break in comments)

declare @ww int

set @ww=1
if @ww = 1
SET @result = 'one'

Sorry for the editting, my rabbit ran across my keyboard.

Upvotes: 1

Md. Mahmudul Hasan
Md. Mahmudul Hasan

Reputation: 51

Your mistake was:: you did not write 'end' after case statement.

Upvotes: 0

Jade
Jade

Reputation: 2992

You can't use Case in stand alone, instead use it like this

declare @ww int
set @ww=1
SELECT CASE @ww 
  WHEN '1' THEN 'one'
ELSE NULL 
end

Upvotes: 1

Related Questions