How to Create Event Triggers in Snowflake

Galaxy Glossary

How do I create and manage event triggers in Snowflake?

Event triggers in Snowflake rely on Streams + Tasks to run SQL automatically when new or changed data appears.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

What are event triggers in Snowflake?

Event triggers are a Streams + Tasks pattern that fires SQL when new rows land or existing rows change. A Stream tracks the change data capture (CDC) for a table, and a Task executes a statement whenever the Stream reports pending rows.

Why use event triggers instead of polling?

Event triggers remove manual polling, drive near-real-time pipelines, and only consume warehouse credits when work exists, saving money and complexity.

How do I create an event trigger that reacts to new orders?

Create a Stream on Orders and a Task that checks system$stream_has_data(). When new orders arrive, the Task inserts summary rows or calls a stored procedure.

CREATE OR REPLACE STREAM orders_stream ON TABLE Orders;
CREATE OR REPLACE TASK process_new_orders
WAREHOUSE = analytics_wh
SCHEDULE = '1 MINUTE'
WHEN system$stream_has_data('orders_stream')
AS
CALL sp_process_orders();

How do I enable and disable an event trigger?

Use ALTER TASK task_name RESUME; to start and SUSPEND; to stop. Suspended tasks keep their definition but won’t consume credits.

How do I monitor and debug event triggers?

Query SNOWFLAKE.ACCOUNT_USAGE.TASK_HISTORY or SHOW TASKS; for last run, status, and errors. Combine with QUERY_HISTORY to view the SQL that executed.

Best practices for event triggers in Snowflake

Use dedicated roles

Grant only the minimum privileges needed to the Task owner to follow least-privilege principles.

Keep statements idempotent

Design downstream SQL so reruns don’t duplicate data—e.g., use MERGE instead of INSERT.

Set error handling

Include ON_ERROR = 'CONTINUE' or explicit TRY/CATCH blocks inside procedures to prevent infinite failure loops.

Why How to Create Event Triggers in Snowflake is important

How to Create Event Triggers in Snowflake Example Usage


-- 1. Track inserts to Orders
a CREATE OR REPLACE STREAM orders_cdc ON TABLE Orders;

-- 2. Update product stock levels whenever a new order appears
b CREATE OR REPLACE TASK update_product_stock
      WAREHOUSE = analytics_wh
      SCHEDULE  = '1 MINUTE'
      WHEN system$stream_has_data('orders_cdc')
AS
      MERGE INTO Products p
      USING (
        SELECT product_id, SUM(quantity)   AS qty
        FROM   OrderItems oi
        JOIN   orders_cdc   s  ON oi.order_id = s.id
        GROUP  BY product_id
      ) o
      ON p.id = o.product_id
      WHEN MATCHED THEN UPDATE SET stock = stock - o.qty;

How to Create Event Triggers in Snowflake Syntax


-- Create a change stream
a) CREATE [OR REPLACE] STREAM stream_name
     ON TABLE schema.table
     [APPEND_ONLY = TRUE | FALSE]
     [SHOW_INITIAL_ROWS = TRUE | FALSE];

-- Create the task that acts as an event trigger
b) CREATE [OR REPLACE] TASK task_name
     WAREHOUSE = warehouse_name
     SCHEDULE  = { '<interval>' | USING CRON '<cron_expr>' UTC }
     [USER_TASK_TIMEOUT_MS = <milliseconds>]
     WHEN system$stream_has_data('schema.stream_name')
     [AS <single SQL statement> | CALL <stored_procedure>();]

-- Control task state
c) ALTER TASK task_name { RESUME | SUSPEND | SET ... };

Common Mistakes

Frequently Asked Questions (FAQs)

Can a Task call a stored procedure?

Yes. Place CALL my_proc(); in the task body to run multi-step logic.

How do I suspend a Task without losing state?

Run ALTER TASK task_name SUSPEND;. The definition stays intact, and history remains queryable.

Does Snowflake guarantee exactly-once execution?

The platform retries failed runs, so design your SQL to be idempotent (e.g., use MERGE) to avoid duplicates.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.