我需要帮助编写条件where子句.这是我的情况:
我有一个值来确定在select语句中返回哪些行.如果值为true,我需要返回import_id列不为null的行,如果为false,则我需要import_id列为null的行.
我尝试这样的查询(下面)似乎不起作用,实现这一目标的最佳方法是什么?
DECLARE @imported BIT
SELECT id, import_id, name FROM Foo WHERE
(@imported = 1 AND import_id IS NOT NULL)
AND (@imported = 0 AND import_is IS NULL)
谢谢.
更改AND
到OR
DECLARE @imported BIT SELECT id, import_id, name FROM Foo WHERE (@imported = 1 AND import_id IS NOT NULL) OR (@imported = 0 AND import_is IS NULL)
你基本上写的
@imported = 1 AND import_id IS NOT NULL AND @imported = 0 AND import_is IS NULL
相当于
@imported = 1 AND @imported = 0 AND import_id IS NOT NULL AND import_is IS NULL
是什么导致了两对完全否定对方的条款