Run Update SQL Query with If Clause

Posted on the 25 May 2012 by Akahgy

Description:

Running an SQL script that performs a update depending on some given conditions.

Solution:

SQL query can take if clauses in the shape of cases:

UPDATE table
SET name = case
WHEN id = 1 then ‘John’
ELSE null
END,
SET name = case
WHEN id = 2 then ‘Mark’
ELSE null
END
WHERE  reference != 0;