开发者

If not exists statement in sql server 2005

开发者 https://www.devze.com 2022-12-17 22:59 出处:网络
Ho开发者_如何学运维w to write if not exists statement in sql server 2005 for the following condition

Ho开发者_如何学运维w to write if not exists statement in sql server 2005 for the following condition

If @MeasurementId say values (1,2,3) those are the existing values the below statement work but when @MeasurementId=0 it doesn't seem to work...

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

However the above statement doesn't seem to work if have NULL values in MeasurementId column. Mind you MeasurementId is not a ForeignKey here...

How to assign NULL if @MeasurementId is 0 or use @MeasurementId as it is in the where statement...

This is my MaterialQuantity table


Unless I've misunderstood, I think you're after the ISNULL syntax:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号