I struggled to get this to work, because it is pretty strict in adding half columns to the right places. But once you get used to it, it works well. Besides the inability to return records, of course, you can create notifications and exceptions and perform other workarounds, for example, using temporary tables, as indicated in the comment by @ErwinBrandstetter in the comment above.
eg:.
DO $$ BEGIN IF EXISTS(SELECT 'any rows?' FROM {your_table} WHERE {your_column} = 'blah') THEN RAISE NOTICE 'record exists'; ELSE RAISE EXCEPTION 'record does not exist'; END IF; DROP TABLE IF EXISTS foo; CREATE TEMP TABLE foo AS SELECT 'bar'::character varying(5) as baz; END $$; SELECT * FROM foo;
Davos Dec 6 '17 at 4:06 on 2017-12-06 04:06
source share