escaping quotes in execute insert statment

折月煮酒 提交于 2019-12-24 12:43:50

问题


I have a function that has an insert inside within loop. See the function below.

        create temp table temp2 (id serial, other_value uuid);
        CREATE OR REPLACE function verify_uuid() returns varchar AS $$
        declare uu RECORD;
        BEGIN
            FOR uu IN  select * from temp1 
            loop
            execute 'INSERT INTO temp2 values ''' || uu ||''':uuid';
            END LOOP;
        END
        $$
        LANGUAGE 'plpgsql' ;
        select verify_uuid();

The problem that I am having is the values part. With its present setup, I am getting the error:

QUERY: INSERT INTO temp2 values '(1,6f32e71c-9aad-48a9-a72c-bdec2f4548a2)':uuid

The quotes are in the wrong place, and I am not sure how to get them in the right place.


回答1:


So in the end, I went with the following. It got me to this point:

EXECUTE 'INSERT INTO temp2 values ('||uu.id||','''|| uu.some_value||''')';




回答2:


execute
    'insert into temp2 (other_value) values ($1)'
    using uu.the_column::uuid;

http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN



来源:https://stackoverflow.com/questions/23231508/escaping-quotes-in-execute-insert-statment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!