SQL案例语句语法错误

时间:2014-05-28 18:58:05

标签: sql sql-server-2008r2-express

我是SQL Server新手并做一个教程,但似乎无法找到错误。它正如教程所暗示的那样,但不会执行。见下面的错误。是否需要打开某些功能?我正在使用SQL Server 2008R2 Express。那有关系吗?

use AdventureWorks
go

SELECT top 5 Name, GroupName
 CASE GroupName
    when 'Research and Development' then 'RD'
    when 'Sales and Marketing' then 'SM'
    else 'Other'
 END 
FROM HumanResources.department;
go

错误是......

  

Msg 156,Level 15,State 1,Line 3
  关键字'case'附近的语法不正确。

1 个答案:

答案 0 :(得分:3)

你忘了你的逗号。列需要以逗号分隔,并且由于case语句的设置方式,它被视为列。在您的情况下,逗号介于GroupName列和Case语句之间,我将其设为myCase

use AdventureWorks
go

SELECT top 5 Name, GroupName, 
    CASE GroupName
         when 'Research and Development' then 'RD'
         when 'Sales and Marketing' then 'SM'
         else 'Other'
 END as myCase
FROM HumanResources.department;
go
相关问题