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.
UPDATE test
SET Test1 = COALESCE(test1, 'hello'),
Test2 = COALESCE(test2, 'world')
WHERE test1 IS NULL OR
test2 IS NULL