Last active
March 25, 2019 07:12
-
-
Save varunchitale/dd6f1e63f5c3789969061e576e10c24d to your computer and use it in GitHub Desktop.
Update a table and skip the conflicting rows/take some action.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO $$ | |
DECLARE r RECORD; | |
BEGIN | |
FOR r IN <query that will return a record set for r to iterate over> | |
LOOP | |
BEGIN | |
RAISE NOTICE 'Dealing with ID: %',r.id; | |
UPDATE <table> as t1 | |
SET var1 = t2.var1 | |
from <table2> t2 | |
WHERE <constraints>; | |
--Exception handling | |
EXCEPTION WHEN unique_violation THEN | |
RAISE NOTICE 'Skipping conflict at: %', r.id; | |
--Do something else | |
END; | |
END LOOP; | |
END$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment