我运行了以下代码,但NULL未显示在最终查询结果中。 我可以看到NULL被插入临时表中。当我在查询中调用变量时,不会显示NULL值。
USE [AdventureWorks2014];
GO
SET NOCOUNT ON;
SET STATISTICS IO ON;
DECLARE @values TABLE
(
Value VARCHAR(MAX) NULL
)
INSERT INTO @values
VALUES (NULL), ('Black'), ('Blue'), ('Grey'), ('Multi'), ('Red'), ('Silver'), ('Silver'), ('Black'), ('White'), ('Yellow') --SELECT Value FROM @values;
SELECT Production.Product.Name, Production.Product.ProductNumber, Production.Product.Color, Production.Product.ListPrice, Production.Product.SIZE, Production.Product.[Weight], Production.Product.ProductLine, Production.Product.Class, Production.Product.STYLE, Production.ProductPhoto.ThumbNailPhoto, Production.ProductPhoto.LargePhoto FROM Production.Product INNER JOIN Production.ProductProductPhoto ON Production.Product.ProductID = Production.ProductProductPhoto.ProductID INNER JOIN Production.ProductPhoto ON Production.ProductProductPhoto.ProductPhotoID = Production.ProductPhoto.ProductPhotoID
--BEGIN CALLOUT A
WHERE --Production.Product.Color IS NOT NULL
(Production.Product.Color IN (SELECT Value FROM @values));
--END CALLOUT A
GO
答案 0 :(得分:1)
IN
语句将与(field = val1) OR (field = val2) OR (field = val3)
完全相同地执行。将NULL
放在那里将归结为(field = NULL)
,这自然不起作用。
不要将NULL
插入@values
,请尝试以下操作:
WHERE (Production.Product.Color IN (SELECT Value FROM @values))
OR (Production.Product.Color IS NULL)
答案 1 :(得分:0)
SET NOCOUNT ON;
SET STATISTICS IO ON;
DECLARE @values TABLE
(
Value VARCHAR(MAX) NULL
)
INSERT INTO @values
VALUES (NULL), ('Black'), ('Blue'), ('Grey'), ('Multi'), ('Red'), ('Silver'), ('Silver'), ('Black'), ('White'), ('Yellow') --SELECT Value FROM @values;
SELECT Production.Product.Name, Production.Product.ProductNumber, Production.Product.Color, Production.Product.ListPrice, Production.Product.SIZE, Production.Product.[Weight], Production.Product.ProductLine, Production.Product.Class, Production.Product.STYLE, Production.ProductPhoto.ThumbNailPhoto, Production.ProductPhoto.LargePhoto
FROM Production.Product
INNER JOIN Production.ProductProductPhoto ON Production.Product.ProductID = Production.ProductProductPhoto.ProductID
INNER JOIN Production.ProductPhoto ON Production.ProductProductPhoto.ProductPhotoID = Production.ProductPhoto.ProductPhotoID
--BEGIN CALLOUT A
WHERE --Production.Product.Color IS NOT NULL
(ISNULL(Production.Product.Color,'EMPTY') IN (SELECT ISNULL(Value,'EMPTY') FROM @values));
--END CALLOUT A
GO
答案 2 :(得分:0)
WHERE
exists (SELECT Production.Product.Color INTERSECT SELECT Value FROM @values)