-
-
Save petetronic/c6274f33ffea1c7d1956 to your computer and use it in GitHub Desktop.
PostgreSQL conditional anonymous code block
This file contains 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 updated_rows INT; | |
BEGIN | |
UPDATE some_table | |
SET some_column = 'new' | |
WHERE some_column = 'old'; | |
GET DIAGNOSTICS updated_rows = ROW_COUNT; | |
-- Replace 1 with expected row count | |
IF updated_rows <> 1 THEN | |
RAISE EXCEPTION 'Unexpected number of rows would be modified = %', updated_rows; | |
ELSE | |
RAISE NOTICE 'Success. Updated % row(s)', updated_rows; | |
END IF; | |
END; | |
$$; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍