SQL Server CASE然后声明错误

时间:2015-04-08 12:55:49

标签: sql sql-server case

我有一个非常简单的存储过程,我试图在case then语句中添加Where语句。我不断收到错误消息:

  

Msg 156,Level 15,State 1,Procedure Proc_AssuranceBilling_rpt,Line 36
  关键字“Case”附近的语法不正确。

我不知道为什么。有人可以看看并告诉我我做错了什么吗?

WHERE 
   e.[DDEventDesc] IN (SELECT @rptType
           Case WHEN 'Payroll - Audit' THEN ('Payroll - Audit')
                WHEN 'Audits' Then ('Audit - Aup', 'Audit - EBP', 'Audit- Financial Institutions','Audit - Governmental','Audit - HUD','Audit - Not-for-Profit','Audit - Personal Property Tax','Audit - Single Audit','Audit - Small Business')
                WHEN 'Review & Comps' Then ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc')
                WHEN 'Assur Tax Returns' THEN ('5500','720-PCORI','8955-SSA','Campaign Report','Corporate (1120-POL)','LM-1','LM-2','LM-3','LM-4','LM-10','LM-30','Non-Profit (990)','Non-Profit (990 EZ)','Non-Profit (990-N)','Non-Profit (990-T)','Schedule C Letters','Section 104D Letter')
           END) 
  AND AR.[ARType] = 1 
  AND (CLT.[cmaster] = 1 OR CLT.[cinvIndivEng] = 0)

2 个答案:

答案 0 :(得分:4)

我认为您需要将WHERE子句重写为:

WHERE ((@rptType = 'Payroll - Audit' AND e.[DDEventDesc] = 'Payroll - Audit') OR
      (@rptType = 'Audits' AND e.[DDEventDesc] IN ('Audit - Aup', 
                                                   'Audit - EBP', 
                                                   'Audit- Financial Institutions',
                                                   'Audit - Governmental',
                                                   'Audit - HUD',
                                                   'Audit - Not-for-Profit',
                                                   'Audit - Personal Property Tax',
                                                   'Audit - Single Audit',
                                                   'Audit - Small Business') OR
      (@rptType = 'Review & Comps' AND e.[DDEventDesc] IN  ('Audit - Review', 'Audit -Comp/Disc','Audit - Comp w/o Disc')) OR
      (@rptType = 'Assur Tax Returns' AND e.[DDEventDesc] IN ('5500','720-PCORI','8955-SSA','Campaign Report','Corporate (1120-POL)','LM-1','LM-2','LM-3','LM-4','LM-10','LM-30','Non-Profit (990)','Non-Profit (990 EZ)','Non-Profit (990-N)','Non-Profit (990-T)','Schedule C Letters','Section 104D Letter') )
   AND AR.[ARType] = 1 
   AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)

CASE 不能用于返回多个值。来自CASE上的MSDN:

<强>语法:

Simple CASE expression: 
CASE input_expression 
     WHEN when_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END 
Searched CASE expression:
CASE
     WHEN Boolean_expression THEN result_expression [ ...n ] 
     [ ELSE else_result_expression ] 
END
  

THEN result_expression 表达式返回时是   input_expression等于when_expression求值为TRUE,或   Boolean_expression的计算结果为TRUE。 结果表达式是有效的   表达

任何有效的表达式是什么?根据{{​​3}}:

  

是SQL Server数据库的符号和运算符的组合   引擎评估以获得单一数据值

答案 1 :(得分:-1)

变量@rptType应该是大小写变量..你把它放在案例

旁边

试试这个..

      WHERE e.[DDEventDesc] IN ( SELECT Case @rptType  
WHEN 'Payroll - Audit' THEN ('''Payroll - Audit''')
      WHEN 'Audits' Then ('''Audit - Aup, Audit - EBP'', ''Audit- Financial Institutions'',''Audit - Governmental'',''Audit - HUD'',''Audit - Not-for-Profit'',''Audit - Personal Property Tax'',''Audit - Single Audit,Audit - Small Business''')
      WHEN 'Review & Comps' Then ('''Audit - Review'', ''Audit -Comp/Disc'',''Audit - Comp w/o Disc''')
      WHEN 'Assur Tax Returns' THEN ('''5500'',''720-PCORI'',''8955-SSA'',''Campaign Report'',''Corporate (1120-POL)'',''LM-1'',''LM-2'',''LM-3,''LM-4,''LM-10'',''LM-30'',''Non-Profit (990)'',''Non-Profit (990 EZ)'',''Non-Profit (990-N)'',''Non-Profit (990-T)'',''Schedule C Letters'',''Section 104D Letter''')
 END
 ) 
 AND AR.[ARType] = 1 
       AND (CLT.[cmaster]=1 OR CLT.[cinvIndivEng] = 0)