鉴于此输入:
create or replace table t asselect parse_json($1) my_jsonfrom values('{ "FruitShape":"Round", "FruitSize":55 } '),('{ "FruitShape":"Square" } '),('{ "FruitShape":"Oblong", "FruitSize":22, "FruitColor":"Chartreuse" } ');此查询将生成动态SQL:
select 'select ' || (select listagg(distinct 'my_json:'||key::text, ',') from t, lateral flatten(input=>t.my_json, mode=>'OBJECT')) || ' from t;';
生成的SQL及其输出:
select my_json:FruitShape, my_json:FruitSize, my_json:FruitColor from t;MY_JSON:FRUITSHAPE | MY_JSON:FRUITSIZE | MY_JSON:FRUITCOLOR-------------------+-------------------+-------------------"Round" | 55 | NULL "Square"| NULL | NULL "Oblong"| 22 | "Chartreuse"
此存储过程将执行动态SQL,而无需剪切和粘贴:
create or replace procedure p()returns stringlanguage javascriptstrictexecute as calleras$$ const statement1 = ` select 'select ' || (select listagg(distinct 'my_json:'||key::text, ', ') from t, lateral flatten(input=>t.my_json, mode=>'OBJECT')) || ' from t' ` const rs1 = snowflake.execute ({sqlText: statement1}) rs1.next() const statement2 = rs1.getColumnValue(1) const rs2 = snowflake.execute ({sqlText: statement2}) return 'SUCCESS'$$;然后,您可以调用存储过程并收集结果:
call p();select * from table(result_scan(-2))
您提到了根据某些元数据来限制输出。您可以在动态SQL中执行此操作,例如,通过过滤不同的字段列表。
感谢davidgarrison的result_scan()技术!
希望对您有所帮助。



