【问题标题】:PostgreSQL: column names into array PL/pgSQLPostgreSQL:列名到数组 PL/pgSQL
【发布时间】:2015-09-12 16:49:48
【问题描述】:
create or replace function extr( tabname text ) returns text[] as
$$
declare cols text[];
begin
    execute 'array(select column_name::text from information_schema.columns where table_name = '||quote_literal(tabname)||');' into cols;
    return cols;
end;
$$
language 'plpgsql';

select extr('test');

提供一个表名并希望将其列名作为数组返回。上面的代码给出了“数组”或附近的语法错误。如何解决这个问题?

【问题讨论】:

    标签: arrays postgresql plpgsql


    【解决方案1】:

    查询应该以select 开头,而不是array 并且不必是动态SQL。

    试试这个修改后的版本:

    create or replace function extr( tabname text ) returns text[] as
    $$
    declare cols text[];
    begin
        select array(select column_name::text from information_schema.columns
          where table_name = tabname) into cols;
        return cols;
    end;
    $$
    language 'plpgsql';
    

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2012-03-27
      • 2012-12-27
      • 2012-07-28
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 2015-02-26
      • 2012-12-17
      相关资源
      最近更新 更多