有没有办法以下列方式使用变量?
DECLARE @ID int;
SET @ID = NULL;
SELECT *
FROM Market market
WHERE market.ID IS @ID
或者还有另一种方法吗?
感谢。
答案 0 :(得分:12)
你需要做
SELECT *
FROM Market market
WHERE market.ID = @ID OR (@ID IS NULL AND market.ID IS NULL)
目前仍然可以完成
SET ANSI_NULLS OFF
SELECT *
FROM Market market
WHERE market.ID = @ID
但你不应该。不推荐使用此选项。 Quite an interesting related blog post
答案 1 :(得分:2)
是否有一个特殊原因是您试图将null粘贴到变量中而不是仅仅执行:
SELECT *
FROM Market market
WHERE market.ID IS NULL
答案 2 :(得分:0)
如果您只是想确定market.ID是否为null,那么您可以这样做
SELECT *
FROM Market market
WHERE market.ID IS NULL
如果您正在测试以查看market.ID是否为null或其他值,请尝试此
SELECT *
FROM Market market
WHERE market.ID IS NULL
OR market.ID = @ID