微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

再次执行 PostgreSQL 查询的结果以获得最终结果集?

如何解决再次执行 PostgreSQL 查询的结果以获得最终结果集?

我正在寻找一种方法来动态获取所有 Postgresql 表中所有 json 属性的列表。

我有查询 1,它会生成一个 sql 语句列表,然后运行该 sql 语句以一次性获得最终输出(就像 sql Server 中的动态 sql 概念一样)。

查询 1 如下所示:


create temporary table test (ordr int,field varchar(1000));

-- Step 1 Create temp table to insert all table/col/json attrbute info

insert into test(ordr,field)
    
    select 0 ordr,'create temporary table temp_table 
    (    table_schema varchar(200),table_name varchar(200),column_name varchar(200),json_attribute varchar(200),data_type   varchar(50)
    );' 
    
    union

-- Non json type columns

   select 1 ordr,'insert into temp_table(table_name,column_name,data_type,json_attribute)'
   
   union

-- Json columns with data like json object

    select 
        3 ordr,concat('select distinct ''',t.table_name,''' tbl,''',c.column_name,''' col,c.data_type,''' data_type,','jsonb_object_keys(',') json_attribute',' from ',' where jsonb_typeof(',') =  ''object'' union') AS field
    from information_schema.tables t
    join information_schema.columns c on c.table_name = t.table_name
    where t.table_schema not in ('information_schema','pg_catalog')
        --and table_type = 'BASE TABLE'
        and c.data_type ='jsonb';


--final sql statements to build temp table
--copy all the column "txt" to a separate window and execute it,it will create a temp table "temp_table" which will have all tables/cols/json attributes

    select ordr,(case when t.ordr = (select max(t2.ordr) from test t2) then replace(field,'union','') else field end) txt
    from test t
    union    
    select 9999,';select * from temp_table;'
    order by 1 ; 

查询 1 输出:这是一个 sql 语句列表

enter image description here

我正在寻找一种方法来运行查询 1 和查询 1 的输出,这样我就可以一次性获得最终输出

非常感谢任何领导或指导。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。