sql - returning value from execute query string in plpgsql -
i'm trying return id function this:
create or replace function fx (mytable text, myvalue text) returns int $$ declare sqlquery text; resultid int; begin sqlquery := 'insert ' || mytable || ' (id, some_column) values' || ' (' || 'nextval(' || quote_literal('seq_' || mytable) || ')' || ',' || myvalue || ') returning id ' || resultid; execute sqlquery; return resultid; end; $$ language plpgsql; select fx('some_table', 'some_value');
but not working.
how can id sql query string executed?
create or replace function fx (mytable text, myvalue text) returns int $body$ declare sqlquery text; resultid int; begin sqlquery := format ($$ insert %i (id, some_column) values ($1, $2) returning id $$, mytable ); execute sqlquery resultid using nextval('seq_' || mytable), myvalue; return resultid; end; $body$ language plpgsql;
use %i
format
specifier pass identifiers , execute using
pass data parameters.
Comments
Post a Comment