我正在尝试构建一个Postgres函数,它以表名作为变量参数并输出查询,返回来自information_schema的select字符串,如
SELECT *
FROM table1 
UNION 
SELECT *
FROM table2到目前为止,我做了以下几点:
CREATE OR REPLACE FUNCTION query_tables(VARIADIC list text[])
    RETURNS text
AS $$
DECLARE
    qry TEXT;
BEGIN
    SELECT string_agg(
        format('SELECT *
            FROM %1$I', table_name),
        ' UNION ')
    INTO qry
    FROM information_schema.tables
    WHERE table_schema = 'public'
          AND table_name IN (list);
    return qry;
END;
$$所以它可以被称为SELECT * FROM query_tables('table1','table2');
但是,我被困在IN (list)中,无法在IN()中传递这个可变参数。
有可能吗?
发布于 2018-01-11 14:25:20
若要使用数组,请更改
table_name IN (list)到table_name = ANY (list)
https://www.postgresql.org/docs/current/static/functions-comparisons.html
表达式运算符任意(数组表达式)
https://stackoverflow.com/questions/48209168
复制相似问题