- You'd need a space between
tablename
andset
. - You need sinlge quotes around the value
'test'
.
Or if it's supposed to be a column name, you don't need quotes at all. - You need to sanitize identifiers to avoid SQL injection.
Use instead (complete rewrite):
CREATE OR REPLACE FUNCTION garscratch.update_table(_tbl regclass)
RETURNS void AS
$func$
BEGIN
EXECUTE 'UPDATE ' || _tbl || $$ SET id = left(id, 2) WHERE name = 'test'$$;
END
$func$ LANGUAGE plpgsql;
Call:
SELECT garscratch.update_table('myschema.my_table');
Detailed explanation:
Table name as a PostgreSQL function parameter