【发布时间】:2019-01-23 18:13:37
【问题描述】:
我需要根据 SSRS 报告参数的值创建一个 cte。附加代码中的 If 语句在 cte 外执行得很好,但在 cte 内执行错误。
我和我的一位同事花了几个小时试图解决这个问题,但没有成功。我尝试过使用 CASE 语句。我尝试将 WITH cteIngnoreCommID AS 放在 IF 和 ELSE 中,但没有任何效果。 此 cte 将在创建另一个 cte 的后续代码中加入。希望有人能告诉我我需要做什么。
--Declare and set Report parameter for testing purposes only
Declare @IgnoreYear as varchar(4)='2010'
;
WITH cteIgnoreCommID as (
IF @IgnoreYear=''
-- create cte with one record have blank values
Select '' as IgnoreComm_ID
,'' as IgnoreYear
,0 as IngnoreBalFwd
ELSE
-- create cte with records obtained from multiple tables.
-- for proof of concept I am just creating one record.
Select '30710000' as IgnoreComm_ID
,@IgnoreYear as IgnoreYear
,5000 as IngnoreBalFwd
)
--The above code run just fine if you comment out the cte lines.
如果在 cte 之外运行 IF/ELSE,我会得到正确的结果。当使用 cte 编写运行时,出现以下 2 个错误:
Msg 156, Level 15, State 1, Line 5 关键字附近的语法错误 '如果'。消息 102,级别 15,状态 1,第 13 行 ')' 附近的语法不正确。
【问题讨论】: