Tasks
Snowflake · Views, Pipelines & Automation
Theory
A task runs one SQL statement, such as an INSERT, MERGE or CALL to a stored procedure, on a schedule. The schedule is either an interval (SCHEDULE = '5 MINUTE') or a CRON expression with a time zone. A task runs on a warehouse you name, or as a serverless task managed by Snowflake if you omit the warehouse. An optional WHEN clause, such as SYSTEM$STREAM_HAS_DATA('orders_stream'), lets the task skip runs when there is nothing to do.
New tasks are created suspended; you start one with ALTER TASK ... RESUME, which needs the EXECUTE TASK privilege. Tasks can be chained with AFTER to build multi-step pipelines, and TASK_HISTORY shows every run and its result.
Why we use it
Pipelines need to run on their own, without someone pressing run. Tasks give scheduling inside Snowflake, so simple ELT does not need an external scheduler or a server running cron.
When to use it
Use tasks for SQL-based jobs inside Snowflake: incremental loads, cleanups, refreshes. Use an external orchestrator like Airflow when you coordinate many systems outside Snowflake. For simple declarative transformations, consider dynamic tables.
Scheduling a Single Statement
A task schedules one SQL statement or stored procedure call to run automatically on a fixed interval or CRON expression against a chosen warehouse.
Chaining Tasks Together
Tasks can be chained with AFTER so one task's completion triggers the next, letting you build a multi-step pipeline — extract, transform, load — entirely in SQL.
No External Orchestrator Needed
Because chaining and scheduling both live inside Snowflake, simple pipelines don't need a separate tool like Airflow just to run in sequence.
How it works
- CREATE TASK with schedule and SQL
- ALTER TASK ... RESUME (tasks start suspended)
- Schedule fires
- Does the WHEN condition return true? Yes: Run the SQL on the warehouse or serverless compute. No: Skip this run.
- Record the result in TASK_HISTORY
- Wait for the next scheduled time
Example
CREATE OR REPLACE TASK hourly_cleanup
WAREHOUSE = analytics_wh
SCHEDULE = 'USING CRON 0 * * * * UTC'
AS DELETE FROM staging_customers WHERE loaded_at < DATEADD(DAY,-7,CURRENT_TIMESTAMP());
CREATE OR REPLACE TASK daily_summary
USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE = 'XSMALL'
SCHEDULE = 'USING CRON 0 6 * * * UTC'
AS INSERT INTO daily_summary_table
SELECT CURRENT_DATE()-1, COUNT(*), SUM(amount) FROM orders WHERE order_date = CURRENT_DATE()-1;
ALTER TASK hourly_cleanup RESUME;
ALTER TASK daily_summary RESUME;Key points
- Tasks are created suspended; ALTER TASK ... RESUME starts them
- Supports both fixed intervals and CRON expressions with time zones
- Omit WAREHOUSE to make the task serverless
- WHEN SYSTEM$STREAM_HAS_DATA skips runs when no changes exist
- Chain tasks with AFTER to build multi-step pipelines