Reputation: 2045
I want to add uuid into a table using dynamic sql.Hier is my Code:
CREATE OR REPLACE FUNCTION "Surrogate_gen"(tblname text) RETURNS void AS
$BODY$DECLARE
uid UUID;
tablename text;
BEGIN
uid:=(select uuid_generate_v1());
tablename:=tblname;
execute 'INSERT INTO public."'||tablename||'"(surrogate) VALUES('||uid||')';
END
$BODY$
LANGUAGE plpgsql
but an error occured like this: INSERT INTO public."produkt"(surrogate) VALUES(ed520ad0-5aba-11e2-961b-1c4bd605a98d) Syntaxerror: »aba« that is in my uid
If I dont use Dynamic sql It is possible to add uuid in this table. Would please say me why this error occures? Thank you
Upvotes: 0
Views: 384
Reputation: 125244
The easiest is to just move the select
to inside the string:
BEGIN
tablename:=tblname;
execute 'INSERT INTO public."'||tablename||'"(surrogate) select uuid_generate_v1()';
END
Upvotes: 1