Store select query's output in one array in postgres

前端 未结 2 1670
醉梦人生
醉梦人生 2020-12-13 11:54

My code is:

SELECT column_name
FROM information.SCHEMA.columns
WHERE table_name = \'aean\'

It returns column names of table aean

相关标签:
2条回答
  • 2020-12-13 12:27

    There are two ways. One is to aggregate:

    SELECT array_agg(column_name::TEXT)
    FROM information.schema.columns
    WHERE table_name = 'aean'
    

    The other is to use an array constructor:

    SELECT ARRAY(
    SELECT column_name 
    FROM information.schema.columns 
    WHERE table_name = 'aean')
    

    I'm presuming this is for plpgsql. In that case you can assign it like this:

    colnames := ARRAY(
    SELECT column_name
    FROM information.schema.columns
    WHERE table_name='aean'
    );
    
    0 讨论(0)
  • 2020-12-13 12:38

    I had exactly the same problem. Just one more working modification of the solution given by Denis (the type must be specified):

    SELECT ARRAY(
    SELECT column_name::text
    FROM information_schema.columns
    WHERE table_name='aean'
    )
    
    0 讨论(0)
提交回复
热议问题