I need to create a data patching script and I'd like to rollback the transaction when some condition occurs.
I could do something like this in the end of the script:
select t.id / 0
from some_table t
where t.state = 'undersirable state'
but I'd rather have a more appropriate error message than "division by zero".
Is there a generic function for generating errors in PostgreSQL? I'd like to do it without PL/SQL, if possible.
Write a little function that raises the error for you and use that in your SELECT
statement.
CREATE FUNCTION raise_error() RETURNS integer
LANGUAGE plpgsql AS
$$BEGIN
RAISE EXCEPTION /* whatever you want */;
RETURN 42;
END;$$;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With