雪花 while 循环
Snowflake while loop
我正在迁移 SQL 服务器存储过程并使用 DBT 将数据加载到 Snowflake。我在 while 循环中使用插入时遇到一些问题。如果有人对此有反馈意见,我将不胜感激。
错误:SQL编译错误:语法第 7 行位置 18 意外'('。位置 12 处的语法错误行 8 意外 'insert'。
临时表已定义。
execute immediate $$
begin
set firstmonth = '2022-03-01';
set lastmonth = '2022-04-01';
set currmonth = $firstmonth;
while ($currmonth <$lastmonth) do
insert into tmptable( col1, col2)
select 1,2 from tableA;
currmonth = dateadd(month, 1,$currmonth);
end while;
end;
$$
;
所需的语法略有不同,已修复:
execute immediate $$
declare
firstmonth date default '2022-03-01';
lastmonth date default '2022-04-01';
currmonth date default firstmonth;
begin
while (currmonth < lastmonth) do
insert into tmptable( col1, col2)
select 1,2 from tableA;
currmonth := dateadd(month, 1, currmonth);
end while;
return currmonth;
end;
$$
转换代码的一种方法是使用 Snowflake Scripting variables(LET
) instead of session variables(SET
):
begin
let firstmonth DATE := '2022-03-01';
let lastmonth DATE := '2022-04-01';
let currmonth DATE := :firstmonth;
while (currmonth <lastmonth) do
insert into tmptable( col1, col2)
select 1,2 from tableA;
currmonth := dateadd(month, 1,currmonth);
end while;
end;
雪花脚本的赋值运算符是:=
。
我正在迁移 SQL 服务器存储过程并使用 DBT 将数据加载到 Snowflake。我在 while 循环中使用插入时遇到一些问题。如果有人对此有反馈意见,我将不胜感激。
错误:SQL编译错误:语法第 7 行位置 18 意外'('。位置 12 处的语法错误行 8 意外 'insert'。
临时表已定义。
execute immediate $$
begin
set firstmonth = '2022-03-01';
set lastmonth = '2022-04-01';
set currmonth = $firstmonth;
while ($currmonth <$lastmonth) do
insert into tmptable( col1, col2)
select 1,2 from tableA;
currmonth = dateadd(month, 1,$currmonth);
end while;
end;
$$
;
所需的语法略有不同,已修复:
execute immediate $$
declare
firstmonth date default '2022-03-01';
lastmonth date default '2022-04-01';
currmonth date default firstmonth;
begin
while (currmonth < lastmonth) do
insert into tmptable( col1, col2)
select 1,2 from tableA;
currmonth := dateadd(month, 1, currmonth);
end while;
return currmonth;
end;
$$
转换代码的一种方法是使用 Snowflake Scripting variables(LET
) instead of session variables(SET
):
begin
let firstmonth DATE := '2022-03-01';
let lastmonth DATE := '2022-04-01';
let currmonth DATE := :firstmonth;
while (currmonth <lastmonth) do
insert into tmptable( col1, col2)
select 1,2 from tableA;
currmonth := dateadd(month, 1,currmonth);
end while;
end;
雪花脚本的赋值运算符是:=
。