SQL雪花脚本-如何在循环时返回在内存中创建的表? [英] SQL Snowflake Scripting - How can I return a table created in memory while looping?

查看:23
本文介绍了SQL雪花脚本-如何在循环时返回在内存中创建的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL Snowflake脚本中使用游标左右循环结果时,如何返回在内存中创建的表?

我可以创建temp tableinsert into以在末尾返回结果,但这太慢了。

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

推荐答案

在内存中创建表的解决方案是创建一个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;

在我的初始测试中,性能略逊于线性,但比使用临时表要好得多。

(h/t Darren Gardner)

这篇关于SQL雪花脚本-如何在循环时返回在内存中创建的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆