Dynamic column in SELECT statement postgres

后端 未结 6 1629
温柔的废话
温柔的废话 2020-12-20 14:55

I am quite new to the postgresql.

what is the best way to achieve this?

SELECT get_columns() 
  FROM table_name;

get_columns(

6条回答
  •  有刺的猬
    2020-12-20 15:37

    Since you are using COPY FROM to a known large table, CREATE a FUNCTION which returns SETOF bigtable and SELECTs all the columns from the specific type, use NULL AS fieldname for the fields which are not required in that specific case, something like:

    # \d SMALL
         Table "public.small"
     Column |  Type   | Modifiers 
    --------+---------+-----------
     a      | integer | 
     b      | integer | 
     c      | integer | 
     d      | integer | 
    
    # \d LARGE
         Table "public.large"
     Column |  Type   | Modifiers 
    --------+---------+-----------
     a      | integer | 
     b      | integer | 
     c      | integer | 
     d      | integer | 
    
    # CREATE OR REPLACE FUNCTION myData()
     RETURNS SETOF large LANGUAGE SQL AS $$
    SELECT a, 
           CASE WHEN a = 1 
                THEN b 
           ELSE 
                NULL 
    END as b, 
           CASE WHEN a = 2 
                THEN c 
           ELSE 
                NULL
    END AS c, 
    d
    FROM small;
    $$;
    
    # SELECT * FROM mydata();
    # COPY (SELECT * FROM myData()) TO STDOUT;
    

    Obviously SQL might not be the best language to use, so PL/PgSQL or PL/Perl (or whatever) may be appropriate.

提交回复
热议问题