postgresql - Update from stored procedure -
i need create stored procedure can take in schema name , table name parameter , make update on same table.
create or replace function garscratch.update_table(schema_name text, table_name text ) returns void $$ declare table text; begin execute 'update '||schema||'.'||table_name|| 'set id = substring(id 1 2) name = "test"'; end; $$ language plpgsql;
when execute procedure above as:
select update_table(my,my_table);
i error:
column "my" not exist.
it not treat "my" schema name.
- you'd need space between
tablename
,set
. - you need sinlge quotes around value
'test'
.
or if it's supposed column name, don't need quotes @ all. - you need sanitize identifiers avoid sql injection.
use instead (complete rewrite):
create or replace function garscratch.update_table(_tbl regclass) returns void $func$ begin execute 'update ' || _tbl || $$ set id = left(id, 2) name = 'test'$$; end $func$ language plpgsql;
call:
select garscratch.update_table('myschema.my_table');
detailed explanation:
table name postgresql function parameter
Comments
Post a Comment