Jake Jake - 5 months ago 13
SQL Question

SQL Stored Procedure: If variable is not null, update statement

I have an update statement in a stored procedure that looks generally like this:

Update [TABLE_NAME]
Set XYZ=@ABC


Is there a good way to only trigger the update statement if the variable is not null or the value -1?

Similar to an
IF NOT EXISTS...INSERT
question.

Thank you so much.

Answer

Use a T-SQL IF:

IF @ABC IS NOT NULL AND @ABC != -1
    UPDATE [TABLE_NAME] SET XYZ=@ABC

Take a look at the MSDN docs.

Comments