How to get a result from dynamic SQL in Postgres?

前端 未结 2 1078
野性不改
野性不改 2021-01-19 14:54

Raw Table for which rule are stored in one table named md_formula , which are used to map in destination table

Drop/Create/Insert for raw_dbs_transacti

相关标签:
2条回答
  • 2021-01-19 15:07

    So this is what i've tried to attained result as per my requirement. Thanks to @pozs your link to some post was really helpful, Appreciated.

    Solution:

    Create or replace Function gen_Test(query_name refcursor)
    returns refcursor
    as $$
    Declare sql text;
    begin
    sql:=(SELECT 'SELECT '|| string_Agg(col_src,',') ||' FROM ' ||  tbl_src FROM md_formula
    WHERE format='Dbs'
    GROUP BY tbl_src);
    open query_name for execute 
    sql;
    return query_name;
    end;
    $$ language plpgsql;
    
    
    select gen_Test('english');
    fetch all in english;
    

    PS: Appreciated everyone feedback for giving time for this issue.

    0 讨论(0)
  • 2021-01-19 15:08

    For a dynamic query you need to use the 'execute' command.

    EXECUTE dynamic-query-string INTO target-variable...
    

    The manual page for this is here: http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

    HTH

    0 讨论(0)
提交回复
热议问题