代码之家  ›  专栏  ›  技术社区  ›  Felipe Hoffa

SQL雪花脚本-循环时如何返回在内存中创建的表?

  •  0
  • Felipe Hoffa  · 技术社区  · 3 年前

    在SQL Snowflake脚本中,使用游标或其他方式循环结果时,如何返回在内存中创建的表?

    我可以创造一个 temp table insert into 最后返回结果,但这太慢了。

    (摘自一条现已删除的评论) Snowflake Scripting in SQL - how to iterate over the results of a SHOW command? )

    1 回复  |  直到 3 年前
        1
  •  2
  •   Felipe Hoffa    3 年前

    array 然后返回 select * from(flatten(array) :

    declare
      tmp_array ARRAY default ARRAY_CONSTRUCT();
      rs_output RESULTSET;
    begin
        for i in 1 to 20 do
            tmp_array := array_append(:tmp_array, OBJECT_CONSTRUCT('c1', 'a', 'c2', i));
        end for;
        rs_output := (select value:c1, value:c2 from table(flatten(:tmp_array)));
        return table(rs_output);
    end;
    

    enter image description here

    在我最初的测试中,性能比线性的稍差,但比使用临时表要好得多。

    enter image description here