Search code examples
snowflake-cloud-data-platform

Automate execution of Snowflake worksheet


I am quite new to Snowflake and still a SQL novice. I have a Snowflake worksheet with 30+ queries. These queries need to be run from top to bottom as the sequence is important (eg. query #3 relies on the results of #1 and #2, and so on)

I am wondering what the options are for automating the execution of these queries in the current ordered sequence.

Thank you


Solution

  • As some comments have mentioned, you could create a stored procedure, and schedule this with a task.

    create or replace procedure my_proc()
      returns varchar
      language SQL
      as
      $$
      BEGIN
    
        <your_sql_code_here>
    
        return 'done';
      END;
      $$
    

    For more info on Stored Procedures with Snowflake Scripting, take a look here: https://docs.snowflake.com/en/developer-guide/stored-procedure/stored-procedures-snowflake-scripting

    And then create a task to schedule this to happen on a regular basis:

    create task my_task
      warehouse = my_warehouse
      schedule = '60 MINUTE'
    as
    call my_proc()
      
    

    More info on tasks here: https://docs.snowflake.com/en/sql-reference/sql/create-task#examples