Oracle – Update Row Where Associated Row Does Not Exist

mergeoracle

Given the table named ROLE:

role_id[PK] NUMBER
role_name   VARCHAR2

Using only SQL (no PL/SQL) how would I write a MERGE (or UPDATE?) statement that would update the role_name as below, but only for rows where another row with the new role_name does not already exist? (By "new role_name" I mean 'prefix-' || role_name)

UPDATE role
SET role_name = 'prefix-' || role_name
WHERE role_name LIKE 'com.example.%';

Best Answer

Add the predicate:

 AND NOT EXISTS (
 SELECT 1 FROM role AS r_existing 
 WHERE r_existing.role_name = 'prefix-' || role.role_name)