相关疑难解决方法(0)

表名作为PostgreSQL函数参数

我想在Postgres函数中传递一个表名作为参数.我试过这段代码:

CREATE OR REPLACE FUNCTION some_f(param character varying) RETURNS integer 
AS $$
    BEGIN
    IF EXISTS (select * from quote_ident($1) where quote_ident($1).id=1) THEN
     return 1;
    END IF;
    return 0;
    END;
$$ LANGUAGE plpgsql;

select some_f('table_name');
Run Code Online (Sandbox Code Playgroud)

我得到了这个:

ERROR:  syntax error at or near "."
LINE 4: ...elect * from quote_ident($1) where quote_ident($1).id=1)...
                                                             ^

********** Error **********

ERROR: syntax error at or near "."
Run Code Online (Sandbox Code Playgroud)

以下是更改为此时出现的错误select * from quote_ident($1) tab where tab.id=1:

ERROR:  column tab.id does not exist
LINE 1: ...T EXISTS …
Run Code Online (Sandbox Code Playgroud)

postgresql function dynamic-sql plpgsql identifier

69
推荐指数
4
解决办法
7万
查看次数

标签 统计

dynamic-sql ×1

function ×1

identifier ×1

plpgsql ×1

postgresql ×1