From 39d734bb419499cd7c740be2149e191f6b006475 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 11 Oct 2017 00:17:21 -0400 Subject: [PATCH] unwrap the newly created json arrangement and dynamically create a temp table --- srce.pgsql | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/srce.pgsql b/srce.pgsql index cec92cd..0edffb9 100644 --- a/srce.pgsql +++ b/srce.pgsql @@ -1,3 +1,4 @@ +/* SELECT jsonb_pretty( $$ @@ -39,4 +40,35 @@ $$ } } $$::jsonb -) \ No newline at end of file +) +*/ + +DO $$ + +declare _t text; + +begin + + SELECT + string_agg(quote_ident(prs.key)||' '||prs.type,',') + INTO + _t + FROM + TPS.srce + --unwrap the schema definition array + LEFT JOIN LATERAL jsonb_populate_recordset(null::tps.srce_defn_schema, defn->'schema') prs ON TRUE + GROUP BY + srce; + + raise notice '%', _t; + _t := format('CREATE TEMP TABLE csv_i (%s)', _t); + raise notice '%', _t; + + DROP TABLE IF EXISTS csv_i; + + EXECUTE _t; + +end +$$; + +SELECT * FROM csv_i; \ No newline at end of file