CREATE TABLE [dbo].[Table1] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Column1] VARCHAR (3) NULL,
[Column2] VARCHAR (2) NULL,
[Column3] VARCHAR (255) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [TableCon] UNIQUE NONCLUSTERED ([Column1] ASC, [Column2] ASC, [Column3] ASC)
);
我的表将允许列(1-3)中的空值,但每个记录将基于这三个值是唯一的。然后我会有一个函数GetID(c1, c2, c3)
,它将返回Id或无值
我知道我知道这不起作用:
SELECT Id
FROM Table1
WHERE Column1 = @c1
AND Column2 = @c2
AND Column3 = @c3
那我怎么写这样的东西?
答案 0 :(得分:2)
由于null
与true
运算符相比不会返回=
,因此您必须使用IS NULL
运算符明确检查它们:
SELECT Id
FROM Table1
WHERE ((Column1 = @c1) OR (Column1 IS NULL AND @c1 IS NULL)) AND
((Column2 = @c2) OR (Column2 IS NULL AND @c2 IS NULL)) AND
((Column3 = @c3) OR (Column3 IS NULL AND @c3 IS NULL))