Search code examples
snowflake-tasksnowflake-cloud-data-platform

Snowflake event based TASK Scheduling instead of Time Based task scheduling


I'm in need of examples of Snowflake event based TASK Scheduling instead of Time Based task scheduling. I could not find those examples in snowflake document.

thanks in advance


Solution

  • The only event source that can trigger a task is the completion of a prior task in a task tree, see using the "AFTER" parameter.

    CREATE TASK mytask2
      WAREHOUSE = mywh
      AFTER mytask1
    AS
    INSERT INTO mytable2(id,name) SELECT id, name FROM mytable1;
    

    Also, if the event would be an insert or a change to a record in a table, you can create a stream on the table and use the WHEN clause to keep the scheduled task from running until the stream has data.

    create stream mystream on table mytable
    APPEND_ONLY = TRUE; // Set to true to only capture inserts
    
    CREATE TASK mytask1
      WAREHOUSE = mywh
      SCHEDULE = '5 minute'
    WHEN
      SYSTEM$STREAM_HAS_DATA('MYSTREAM')
    AS
      INSERT INTO mytable1(id,name) SELECT id, name FROM mystream WHERE METADATA$ACTION = 'INSERT';
    

    https://docs.snowflake.com/en/sql-reference/sql/create-task.html

    https://docs.snowflake.com/en/sql-reference/sql/create-stream.html