如果sql server 2005中不存在语句

时间:2010-01-26 13:20:29

标签: sql-server-2005 select exists

如何在sql server 2005中为以下条件编写if not exists语句

如果@MeasurementId说值(1,2,3),那么这些是现有值,下面的语句可以正常工作 但是当@ MeasurementId = 0时它似乎不起作用......

If not exists(select Mat_Id,MeasurementId from MaterialQuantity where 
 Mat_Id=@Id and MeasurementId=@MeasurementId)

但是,如果NULL列中包含MeasurementId值,则上述声明似乎不起作用。请注意,MeasurementId在这里不是ForeignKey ......

如果NULL为0或如果使用@MeasurementId,则如何分配@MeasurementId where陈述......

这是我的 MaterialQuantity table

1 个答案:

答案 0 :(得分:3)

除非我误解了,否则我认为你是在ISNULL语法之后:

If not exists(select Mat_Id,MeasurementId from MaterialQuantity where 
 Mat_Id=@Id and ISNULL(MeasurementId, 0) =@MeasurementId)
相关问题