0

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.