I'm new to PostgreSQL (currently on PostgreSQL 13) and may be confusing things from what other SQL encounters I've had (Microsoft SQL).
The goal is to assert there are no values in a table column which would get truncated, then reduce the column length and do the same for the return type of a related function
An example of the code giving the error, so it can be reproduced:
/*
CREATE TABLE test_table (id uuid, col_a varchar(100), col_b int);
INSERT INTO test_table VALUES (gen_random_uuid(), 'asdf', 1);
**/
DO $$
BEGIN
IF NOT EXISTS (SELECT * FROM test_table WHERE character_length(col_a) > 100) THEN
ALTER TABLE test_table ALTER COLUMN col_a TYPE varchar(100);
DROP FUNCTION IF EXISTS test_function(varchar(100));
CREATE OR REPLACE FUNCTION test_function(test_param varchar(100))
RETURNS TABLE (
id uuid,
col_a varchar(100),
col_b int
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT test_table.id AS id, test_table.col_a AS col_a, test_table.col_b AS col_b
FROM test_table
WHERE test_table.col_a = test_param;
END;
$$;
ELSE
RAISE NOTICE 'Cannot Revert. Rows exist that would be truncated!';
END IF;
END $$
SELECT id, col_a, col_b FROM test_function ('asdf');
/* DROP TABLE test_table */
The error I get is:
SQL Error [42601]: ERROR: syntax error at or near "BEGIN" Position: 400
I have tried an attempt at dynamic code, I've run the function statement on its own - sanity check that works; in fact without the IF
block, running each statement either together or separately works fine too.
What's wrong with my approach and how to fix it?