简体   繁体   English

如何使用案例sql server 2008

[英]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...? 我犯错误的地方......?

Please read http://technet.microsoft.com/en-us/library/ms181765.aspx 请阅读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. ". 在那里它声明“CASE可以在允许有效表达式的任何语句或子句中使用。例如,您可以在诸如SELECT,UPDATE,DELETE和SET之类的语句中使用CASE,并在诸如select_list,IN,WHERE之类的子句中使用CASE。 ORDER BY,并且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. 抱歉编辑,我的兔子跑过我的键盘。

You can't use Case in stand alone, instead use it like this 你不能单独使用Case,而是像这样使用它

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

你的错误是::你在案件陈述后没有写'结束'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM