检查列是否具有空值tsql

Joh*_*ohn 3 sql t-sql sql-server

我正在尝试确定产品表中是否存在产品.当我运行这个tsql时,它应该返回1并退出proc,因为产品表中不存在产品ID 7777,但是它返回0.如果我在没有if语句的情况下运行它并只执行select声明,它说的@prodIDnull.我想知道为什么它不会进入if语句,因为我正在检查它是否为空值.在此先感谢您的帮助.

 Declare  @ProdID int

 select @ProdID = dbo.productTbl.ProductID
 from dbo.ProductTbl
 inner join dbo.OrderTbl
 on dbo.ProductTbl.ProductID = dbo.OrderTbl.ProductID
 where dbo.OrderTbl.ProductID = 7777


 if(@ProdID = null)
 begin 
 raiserror('The product does not exist',16,1)
 return 1
 end
 return 0
Run Code Online (Sandbox Code Playgroud)

Kha*_*han 7

在SQL Server中,用于IS NULL检查空值而不是= null.

if(@ProdID IS NULL)
begin 
raiserror('The product does not exist',16,1)
return 1
end
return 0
Run Code Online (Sandbox Code Playgroud)