'关键字' case'附近的语法不正确

时间:2018-03-30 16:35:34

标签: sql sql-server

我收到错误

  

关键字' case'

附近的语法不正确

有谁看到我写错了什么?这是T-SQL。

SELECT 
    @ifAtOffice     
    CASE
       WHEN @ifAtOffice = 1  
          THEN 'Device is still assigned to a client, locate is at Office' AS description, 2 AS resultCode, 
       WHEN @ifAtOffice = 0 
          THEN 'Device is still assigned to a client, locate is not at Office' AS description, 3 AS resultCode, 
    END

2 个答案:

答案 0 :(得分:4)

实际上你有很多问题。这是一个解决方案:

DECLARE @ifAtOffice AS BIT

SET @ifAtOffice = 1

SELECT @ifAtOffice,
     case 
       when @ifAtOffice = 1 then  'Device is still assigned to a client, locate is at Office'       
       when @ifAtOffice = 0 then  'Device is still assigned to a client, locate is not at Office'   
     END AS description, 
     case 
       when @ifAtOffice = 1 then  2 
       when @ifAtOffice = 0 then  3
     END AS resultCode

答案 1 :(得分:1)

 Declare   @ifAtOffice int ;

      set @ifAtOffice = 1;

    SELECT 
    @ifAtOffice  , 
   ( CASE
       WHEN @ifAtOffice = 1  
         THEN 'Device is still assigned to a client, locate is at Office' 
       WHEN @ifAtOffice = 0 
          THEN 'Device is still assigned to a client, locate is not at Office'  
          End )  AS description,
        (  CASE
       WHEN @ifAtOffice = 1  
         THEN 2 
       WHEN @ifAtOffice = 0 
         Then 3 end ) As resultCode