【问题标题】:store result of execute string into temp table将执行字符串的结果存储到临时表中
【发布时间】:2020-04-21 17:08:59
【问题描述】:

在一个函数中,我创建了一个包含 Select Query as 的字符串

SQLSTR:='select col1,col2 from '||_param1||'_'||_param2||' where col1 like ''%'||_pram3;

我想要的是在运行EXECUTE SQLSTR; 命令后将SQLSTR 的结果作为FilterTable 存储到临时表中。

【问题讨论】:

    标签: sql postgresql stored-procedures plpgsql create-table


    【解决方案1】:

    为什么不直接使用 CTAS 语法?

    SQLSTR := 
        'create temp table FilterTable as select col1,col2 from '
            || quote_ident(_param1 || '_' || _param2) 
            ||' where col1 like ''%' || _param3 || '''';
    

    请注意,我还在语句末尾添加了一个缺少的右引号,并使用quote_ident() 作为表名,以防它包含特殊字符。

    【讨论】:

    • 我能否在函数中进一步使用该表来查询存在变量,例如SQLSTR
    • @JayMehta:是的。临时表会一直存在到创建它的数据库会话结束。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多