-
Conditional Updates
In my stored procedure I have a large amount of data that can be updated. What I would like to do is not update anything that is passed into the stored procedure as NULL. How can I accomplish this?
Basically I'm looking for something like this:
Code:
UPDATE [MySchema].[MyTable]
SET
IF (@MyField IS NOT NULL)
BEGIN
MyField = @ MyField
END
WHERE @UserID = UserID
-
Re: Conditional Updates
Not sure I've got the syntax quite right, but here's one method:
Code:
SET MyField =
CASE WHEN (@MyField IS NULL) Then
MyField
ELSE
@MyField
END
-
Re: Conditional Updates
That works perfectly. Why didn't I think of that? Thanks!
-
Re: Conditional Updates
Actually there is a simpler way - just use ISNULL in reverse
Code:
SET MyField = IsNull(@MyField,MyField)