雪花中的 while 循环
我正在 Snowflake 工作表上编写下面的 SQL 脚本,并在下面收到此错误。表格已定义。如有任何反馈,我们将不胜感激。
错误:
第 9 行位置 4 处未捕获类型“STATMENT_ERROR”异常:SQL 编译错误:位置 34 处错误第 162 行无效标识符“CURRMONTH”
execute immediate $$
declare
firstmonth :=(select dateadd(month,case when day(current_timestamp())<=10 then -1 else 0 end,dateadd(month,0,date_trunc('month',current_date()))));
lastmonth :=(select dateadd(month,1,dateadd(month,case when day(current_timestamp())<=10 then -1 else 0 end,dateadd(month,0,date_trunc('month',current_date())))));
currmonth date default firstmonth;
begin
while (currmonth <=lastmonth) do
insert into temptable
select colA, colB, colC, colD
from tableA
where date between currmonth and dateadd(day,-1,dateadd(month,1,currmonth)) and date2 = currmonth
currmonth :=dateadd(month,1,currmonth)
end while;
end;
$$
I am working on a SQL script below on Snowflake worksheets and getting this error below. Tables are defined. Any feedbacks is appreciated.
Error:
Uncaught exception of type 'STATEMENT_ERROR' on line 9 at position 4 : SQL compilation error: error line 162 at position 34 invalid identifier 'CURRMONTH'
execute immediate $
declare
firstmonth :=(select dateadd(month,case when day(current_timestamp())<=10 then -1 else 0 end,dateadd(month,0,date_trunc('month',current_date()))));
lastmonth :=(select dateadd(month,1,dateadd(month,case when day(current_timestamp())<=10 then -1 else 0 end,dateadd(month,0,date_trunc('month',current_date())))));
currmonth date default firstmonth;
begin
while (currmonth <=lastmonth) do
insert into temptable
select colA, colB, colC, colD
from tableA
where date between currmonth and dateadd(day,-1,dateadd(month,1,currmonth)) and date2 = currmonth
currmonth :=dateadd(month,1,currmonth)
end while;
end;
$
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个,将列名从 date 修改为 date1,
try this, modified the column name from date to date1,
您在语句末尾缺少分号。
You are missing semi colon at end of statement.