views:

68

answers:

1

How 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

+2  A: 

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)
AdaTheDev
@AdaTheDev that worked...
Pandiya Chendur