SQL服务器CASE then语句错误
SQL Server CASE then statement error
我有一个非常简单的存储过程,我试图在 Where
语句中添加 case then
语句。我不断收到错误消息:
Msg 156, Level 15, State 1, Procedure Proc_AssuranceBilling_rpt, Line 36
Incorrect syntax near the keyword '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)
变量@rptType 应该是 case 变量.. 你把它放在 case 外面
试试这个..
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)
我认为您需要将 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
不能用于return多个值。来自 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 Is the expression returned when
input_expression equals when_expression evaluates to TRUE, or
Boolean_expression evaluates to TRUE. result expression is any valid
expression.
任何有效的表达式可以是什么?根据Expressions:
Is a combination of symbols and operators that the SQL Server Database
Engine evaluates to obtain a single data value.
我有一个非常简单的存储过程,我试图在 Where
语句中添加 case then
语句。我不断收到错误消息:
Msg 156, Level 15, State 1, Procedure Proc_AssuranceBilling_rpt, Line 36
Incorrect syntax near the keyword '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)
变量@rptType 应该是 case 变量.. 你把它放在 case 外面
试试这个..
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)
我认为您需要将 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
不能用于return多个值。来自 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 Is the expression returned when input_expression equals when_expression evaluates to TRUE, or Boolean_expression evaluates to TRUE. result expression is any valid expression.
任何有效的表达式可以是什么?根据Expressions:
Is a combination of symbols and operators that the SQL Server Database Engine evaluates to obtain a single data value.