SQL检查变量中的空值

SJM*_*Man 5 sql-server null

我试图将一个表值与一个变量进行比较null,但是因为我们无法将其等于null,因此这是无法比较的.

declare @test varchar(33) -- This can or cannot be NULL;
select * from [dbo].[ViewsConfiguration] where PolicyId= 139 and EventName= @test
Run Code Online (Sandbox Code Playgroud)

这可以使用switch或者if寻找更优雅的方式来完成.

ugh*_*hai 1

您可以使用ISNULL / COALESCE。你的查询就像

declare @test varchar(33) -- This can or cannot be NULL;
select * from [dbo].[ViewsConfiguration] where PolicyId= 139 and ISNULL(EventName,'')= ISNULL(@test,'')
Run Code Online (Sandbox Code Playgroud)