首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >雪花CRON任务未将数据插入表

雪花CRON任务未将数据插入表
EN

Stack Overflow用户
提问于 2022-09-03 07:40:53
回答 1查看 88关注 0票数 0
  1. 如果尝试插入没有任务的表,那么它是成功的,但是任务没有将数据插入到表中。
  2. 任务状态成功,但表为空。
  3. 任务计划每5分钟使用CRON运行一次。

我不知道哪里出了问题。

代码语言:javascript
运行
复制
use database test_db;
use schema test_Schema;
use warehouse test_wh;

--to get the past one year date when I execute the code
set l_last_control_dt =

(
select
ifnull( dateadd( hour, -4, max( start_time ) ), dateadd( month, -13, current_timestamp() ) ) as last_control_dt
from
test_db.test_Schema;.warehouse_metering_history
);

----count of rows where start time from warehouse_metering_history greater than l_last_control_dt 
set l_row_count =

(
select count(*)
from
snowflake.account_usage.warehouse_metering_history where
s.start_time >= to_timestamp( $l_last_control_dt )
);


CREATE  or replace TASK task_load_warehouse_metering_tbl
WAREHOUSE = test_wh
SCHEDULE  = 'USING CRON */5 * * * * UTC'
AS
INSERT into
    test_db.test_schema.warehouse_metering_tbl
(
select
         current_account()                  as account_name
        ,current_region()                   as region_name
        ,s.start_time
        ,s.end_time
        ,s.warehouse_id
        ,s.warehouse_name
        ,s.credits_used
        ,s.credits_used_compute
        ,s.credits_used_cloud_services
        ,'warehouse_metering_history'
        ,getvariable('L_ROW_COUNT')
        ,to_timestamp( sysdate() )
    from
        snowflake.account_usage.warehouse_metering_history s
    where
        s.start_time >= to_timestamp( getvariable('L_LAST_CONTROL_DT') ));

任务成功状态:

运行3次任务后

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-03 07:56:21

会话变量只适用于它在其中创建的会话。

SQL变量是会话的私有变量。当雪花会话关闭时,将删除在会话期间创建的所有变量。这意味着没有人可以访问在另一个会话中设置的用户定义的变量,并且当会话关闭时,这些变量将过期。

任务使用自己的会话执行,因此:

代码语言:javascript
运行
复制
where  s.start_time >= to_timestamp( getvariable('L_LAST_CONTROL_DT') ));

是与空值的比较:

代码语言:javascript
运行
复制
where s.start_time >= NULL

这总是不正确的,选择0行。

对一个全新的会话进行快速测试:

代码语言:javascript
运行
复制
SELECT getvariable('L_LAST_CONTROL_DT') 
-- NULL

当使用$访问会话变量时,可以更容易地发现

代码语言:javascript
运行
复制
SELECT $L_LAST_CONTROL_DT
-- Error: Session variable '$L_LAST_CONTROL_DT' does not exist (line 1)

使用雪花脚本块和块变量的解决方案

代码语言:javascript
运行
复制
CREATE  or replace TASK task_load_warehouse_metering_tbl
WAREHOUSE = test_wh
SCHEDULE  = 'USING CRON */5 * * * * UTC'
AS
DECLARE
    l_last_control_dt TIMESTAMP := (
       SELECT 
           ifnull( dateadd( hour, -4, max( start_time ) ),
                   dateadd( month, -13, current_timestamp() ) ) as last_control_dt
       FROM test_db.test_Schema.warehouse_metering_history);

   l_row_count INTEGER;
BEGIN
   l_row_count := (SELECT COUNT(*)
                   FROM snowflake.account_usage.warehouse_metering_history
                   WHERE s.start_time >= :l_last_control_dt);

   INSERT INTO test_db.test_schema.warehouse_metering_tbl
   -- (col_name1, col_name2, col_name3, ...)
   SELECT
         current_account()                  as account_name
        ,current_region()                   as region_name
        ,s.start_time
        ,s.end_time
        ,s.warehouse_id
        ,s.warehouse_name
        ,s.credits_used
        ,s.credits_used_compute
        ,s.credits_used_cloud_services
        ,'warehouse_metering_history'
        ,:l_row_count 
        ,to_timestamp( sysdate() )
    FROM  snowflake.account_usage.warehouse_metering_history s
    WHERE s.start_time >= :l_last_control_dt;
END;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73590494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档