无法找到合适的例子(或者我只是在寻找错误的方向)。
根据传入的参数值,我需要更改WHERE
的{{1}}条件。
例如,我得到参数SELECT
,其值为'全部'我需要做这样的事情:
@bookType
看起来很简单,但我在WITH里面需要这个IF:
IF @bookType = 'All'
SELECT * FROM tBooks
WHERE BookType != 'Template' AND BookGroup='Library'
ELSE
SELECT * FROM tBooks
WHERE BookType = @bookType AND BookStatus=@bookStatus
这是行不通的,因为我开始接收WITH Books AS (
IF...
...
), bookIds AS (
...
...
)
。究竟我做错了什么?或者也许可以在'Incorrect syntax near IF'
语句中隐藏IF
(根据参数值改变)?
答案 0 :(得分:4)
;
WITH Books AS (
SELECT * FROM tBooks
WHERE
(@BookType = 'All' AND BookType != 'Template' AND BookGroup='Library')
OR
(@BookType <> 'All' AND BookType = @bookType AND BookStatus=@bookStatus)
)