Kabi
Kabi

Reputation: 2045

Error while adding UUID into a table via dynamic sql

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

Answers (1)

Clodoaldo Neto
Clodoaldo Neto

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

Related Questions