我的WHERE子句中有一行,这是一个不错的功能,可以切换。这样,最终用户不必运行两个报告。我还没有找到类似的东西,所以我想你不能,但是也许我以错误的方式看待问题,而别人知道更好的方法。
我尝试运行下面的代码,最后一行为@AND
,并将变量定义为'AND' & '--'
,这给了我错误的语法错误x2。
我还尝试了IS @NULL
并将变量定义为'NULL' & 'NOT NULL'
,这给了我错误的语法错误x2
SELECT
item_id
FROM
cba_item_location
WHERE
discontinued='Y'
AND qty_on_hand = '0'
AND (select abc_code from abc_miscdata where abc_code=item_id and abc_type='ITEM' and data_1 ='DISCON_ZERO_SENT') IS NULL
order by item_id
示例1:
SELECT
item_id
FROM
cba_item_location
WHERE
discontinued='Y'
AND qty_on_hand = '0'
**@AND** (select abc_code from abc_miscdata where abc_code=item_id and abc_type='ITEM' and data_1 ='DISCON_ZERO_SENT') IS NULL
示例2:
SELECT
item_id
FROM
cba_item_location
WHERE
discontinued='Y'
AND qty_on_hand = '0'
AND (select abc_code from abc_miscdata where abc_code=item_id and abc_type='ITEM' and data_1 ='DISCON_ZERO_SENT') IS **@NULL**
有什么想法吗?
答案 0 :(得分:0)
选项1:动态SQL
选项2:在WHERE子句中包含一个标志,它将打开和关闭该标志
DECLARE @and bit = 0
IF (some condition) SET @and = 1
...
WHERE
discontinued='Y'
AND qty_on_hand = '0'
AND (@and = 1 AND (select abc_code from abc_miscdata where abc_code=item_id and abc_type='ITEM' and data_1 ='DISCON_ZERO_SENT') IS NULL)