liveBook · Manning

Snowflake Scripting block

As explained in Snowflake Data Engineering

Snowflake Scripting is Snowflake's procedural SQL language for writing blocks of statements with control flow and variables.

Snowflake Scripting provides procedural SQL blocks for encapsulating data transformations in stored procedures. A procedure can be invoked as one unit by a task or another orchestration mechanism instead of repeatedly issuing individual statements. section 4.3

A Snowflake Scripting-based procedure can combine:

  • SQL transformations such as merging staged records into a target table. section 4.3
  • Programming logic, including branching, looping, exception handling, and logging. section 4.3
  • Scheduled or dependency-aware execution through Snowflake tasks. section 1.1.2

Encapsulating an order load

A stored procedure named LOAD_CUSTOMER_ORDERS can encapsulate a MERGE from ORDERS_COMBINED_STG into CUSTOMER_ORDERS_COMBINED, updating matched orders and inserting new ones. A task or other orchestrator can then call the procedure when new staged data arrives. section 4.3

Listing 4.5 Merging customer orders from the staging table into the target table
merge into CUSTOMER_ORDERS_COMBINED tgt
using ORDERS_COMBINED_STG as src
on src.customer = tgt.customer 
  and src.delivery_date = tgt.delivery_date 
  and src.baked_good_type = tgt.baked_good_type
when matched then 
  update set tgt.quantity = src.quantity, 
    tgt.source_file_name = src.source_file_name, 
    tgt.load_ts = current_timestamp()
when not matched then
  insert (customer, order_date, delivery_date, 
    baked_good_type, quantity, source_file_name, load_ts)
  values(src.customer, src.order_date, src.delivery_date,
    src.baked_good_type, src.quantity, src.source_file_name,
    current_timestamp());

Inspect the executable MERGE baseline that the stored procedure encapsulates, including match conditions, updates, inserts, and load timestamps. section 4.3

How Snowflake Data Engineering covers it

How deep it goes
Explains the syntax needed for a stored procedure and identifies branching, looping, exception handling, and logging without fully detailing each construct.
Examples
Uses a customer-order MERGE that loads combined staging data into a target table.
What you can do after
Build stored procedures that package transformation SQL and invoke them through tasks or other orchestration mechanisms.
How it connects
Connects stored procedures with SQL transformations, tasks, orchestration, exception handling, logging, and Snowpark-supported languages.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Snowflake Data Engineering. The section links point to the parts of the book it draws on.