I have a sql server 2008 table with two nullable decimal values and an ID. I am passing the values and an ID into a stored procedure. These values may be the same as the existing values, or they may be something else. If it is something else, I want to update the existing record. My question is, I’m not sure how to do this because of the nullable factor.
DECLARE @count int
SET @count=(SELECT Count(ID) FROM [MyTable] WHERE
[ID]=@id AND
[Value1]<>@value1 AND
[Value2]<>@value2
)
IF (@count>0)
BEGIN
UPDATE
[MyTable]
SET
[Value1]=@value1,
[Value2]=@value2
WHERE
[ID]=@id
END
My problem is handling the null case. Can somebody please show me how to handle the null case that I described?
Thank you!
Use ISNULL to provide a default value for Value1 and Value2 in your comparison.
And I think you’ll want an OR if you you want to update if either value is different.