【问题标题】:escaping quotes in execute insert statment在执行插入语句中转义引号
【发布时间】:2014-06-07 12:44:59
【问题描述】:

我有一个在循环内插入的函数。请参阅下面的函数。

        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();

我遇到的问题是价值观部分。使用它目前的设置,我得到了错误:

查询:插入到 temp2 值 '(1,6f32e71c-9aad-48a9-a72c-bdec2f4548a2)':uuid

引号放错了位置,我不知道如何将它们放在正确的位置。

【问题讨论】:

  • 我认为 temp1 的 def 可能会有所帮助 -- CREATE temp table temp1 (id serial, some_value varchar);
  • 不熟悉PostGre,但你的问题是你是一个记录。需要类似于执行'INSERT INTO temp2 values (uu.id,''' || uu.uid ||'''):uuid';

标签: postgresql escaping quotes


【解决方案1】:

所以最后,我选择了以下内容。它让我明白了这一点:

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

    【讨论】:

    • 我收到错误 ERROR: column "other_value" is of type uuid but expression is of type character varying LINE 1: INSERT INTO temp2 (other_value) values ($1) ^ HINT: You will need to重写或转换表达式。您将 ::uuid 放在哪里进行类型转换?
    • @Jeff 与演员一起编辑。
    猜你喜欢
    • 1970-01-01
    • 2022-10-13
    • 2022-01-25
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2019-03-09
    相关资源
    最近更新 更多