Snowflake中的动态文件名生成

huangapple go评论66阅读模式
英文:

Dynamic file name generation in snowflake

问题

我想使用存储集成将数据上传到S3存储桶。我的查询看起来像这样:

COPY INTO @storage_int/subfolder/name_${date} FROM (SELECT * FROM table1);

我无法弄清楚如何声明一个变量,使表达式可以评估为storage_int/subfolder/name_2022_13_07,或者像Python中的字符串格式化那样使用f"name_{date}"。对于日期,我想使用CURRENT_DATE()函数。

英文:

I want to upload data to an S3 bucket using a storage integration. My query looks something like that:

COPY INTO @storage_int/subfolder/name_${date} FROM (SELECT * FROM table1);

What I can't figure out is how to declare a variable, such that the expression would evaluate into storage_int/subfolder/name_2022_13_07, or what you would achieve using string formatting in Python with f"name_{date}". For date I would want to use the CURRENT_DATE() function.

答案1

得分: 2

使用Snowflake脚本和动态SQL:

开始
   LET sql := 'COPY INTO @storage_int/subfolder/name_<date> 
               FROM (SELECT * FROM table1);';

   sql := REPLACE(sql, '<date>', TO_VARCHAR(CURRENT_DATE(), 'YYYY_MM_DD'));
   
   EXECUTE IMMEDIATE (sql);

   返回 sql;
结束;
英文:

Using Snowflake Scripting and dynamic SQL:

BEGIN
   LET sql := &#39;COPY INTO @storage_int/subfolder/name_&lt;date&gt; 
               FROM (SELECT * FROM table1);&#39;;

   sql := REPLACE(sql, &#39;&lt;date&gt;&#39;, TO_VARCHAR(CURRENT_DATE(), &#39;YYYY_MM_DD&#39;));
   
   EXECUTE IMMEDIATE (sql);

   RETURN sql;
END;

答案2

得分: 1

根据 @Lukasz 的建议,我使用了 EXECUTE IMMEDIATE 方法,但我使用了管道来构建查询。

DECLARE
    sql VARCHAR(100)
BEGIN
    sql := 'COPY INTO @storage_int/subfolder/name_' || TO_VARCHAR(CURRENT_DATE) || 
           ' FROM (SELECT * FROM table1);';
    EXECUTE IMMEDIATE sql;
END;

这种方法让我能够构建任意字符串,而不必在想要放入字符串的每个变量上使用 REPLACE 函数。

英文:

Following @Lukasz advice I used the EXECUTE IMMEDIATE approach but I built the query using pipes.

DECLARE
    sql VARCHAR(100)
BEGIN
    sql := &#39;COPY INTO @storage_int/subfolder/name_&#39; || TO_VARCHAR(CURRENT_DATE) || 
           &#39; FROM (SELECT * FROM table1);&#39;
    EXECUTE IMMEDIATE sql;
END;

This approach let me build an arbitrary string without having to use REPLACE function on each variable I wanted to put in the string.

huangapple
  • 本文由 发表于 2023年7月13日 21:00:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定