I have the following queries where I am updating values only if they are null.
Is it possible to put all of these into a single query?
UPDATE test
SET test1 = 'hello'
WHERE test1 IS NULL
and
UPDATE test
SET test2 = 'world'
WHERE test2 IS NULL
You could try:
UPDATE test
SET test1 = NVL(test1, 'hello'),
test2 = NVL(test2, 'world')
WHERE test2 IS NULL
OR test1 IS NULL;
Though it may fire your update triggers even for the rows that are effectively unchanged.
UPDATE test
SET test1 = COALESCE(test1, 'hello')
, test2 = COALESCE(test2, 'hello')
WHERE test1 IS NULL OR test2 IS NULL
COALESCE() works similarly to NVL() in this circumstance -- returning the first non-null value.
WHERE user_id = 'myuserid'WHERE clause to test for nulls, rows where both test1 and test2 are not null will needlessly be updated.