redshift - 如何将listagg与information_schema.columns表一起使用

noo*_*ber 11 postgresql amazon-redshift

我正在使用redshift,并希望创建一个以逗号分隔的列列表.我正在尝试使用listagg以下方法从信息模式中获取列名:

SELECT  
listagg(column_name,',') within group (order by ordinal_position) 
FROM    information_schema.columns
WHERE   table_schema = 'my_schema'
AND     table_name = 'my table';
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

[Amazon](500310) Invalid operation: Function (listagg(text,text)) must be applied on at least one user created tables;
Run Code Online (Sandbox Code Playgroud)

Fai*_*aiz 6

这是我测试过的解决方法..

select listagg(column_name, ', ') within group (order by column_name)
from 
(
    select  
        a.attname::varchar as column_name, typname::varchar as data_type
    from
        pg_type t, 
        pg_attribute a,
        pg_class c,
        pg_namespace ns,
        (select top 1 1 from my_schema.my_table)
    where 
        t.oid=a.atttypid
        and a.attrelid = c.oid
        and c.relnamespace = ns.oid
        and typname NOT IN ('oid','xid','tid','cid')
        and attname not in ('deletexid', 'insertxid')
        and trim(relname) = 'my_table'
        and ns.nspname = 'my_schema'
)
Run Code Online (Sandbox Code Playgroud)