SQL SELECT CASE抛出错误,没有括号

时间:2015-05-27 22:07:30

标签: sql sql-server tsql

SELECT CASE s.Country WHEN 1 THEN 'One' WHEN 2 THEN 'Two' 
        WHEN 3 THEN 'Three' ELSE 'Your message.' END 
        ,(SELECT CASE DoYouWishToP When 0 Then 'Yes'  When 1 Then 'No' END)
        ,(SELECT CASE Housingoptions When 'rb0' Then 'Lease'  When 'rb1' then 'im lazy' when 'rb2' Then 'Rental' END)
        from tblSurvey s

上面的脚本确实有效。我的问题是为什么第二和第三个SELECT语句需要在括号内。 ()。你在我的剧本中看到任何问题吗?

如果我在下面使用,我会收到错误:

SELECT CASE s.Country WHEN 1 THEN 'One' WHEN 2 THEN 'Two' 
            WHEN 3 THEN 'Three' ELSE 'Your message.' END 
            ,SELECT CASE DoYouWishToP When 0 Then 'Yes'  When 1 Then 'No' END

1 个答案:

答案 0 :(得分:3)

他们没有。将查询写为:

SELECT (CASE s.Country
              WHEN 1 THEN 'One'
              WHEN 2 THEN 'Two' 
              WHEN 3 THEN 'Three'
              ELSE 'Your message.'
        END),
       (CASE DoYouWishToP When 0 Then 'Yes'  When 1 Then 'No' END),
       (CASE Housingoptions
             When 'rb0' Then 'Lease'
             When 'rb1' then 'im lazy'
             when 'rb2' Then 'Rental'
        END)
from tblSurvey s;