liveBook · Manning

Stored procedure

As explained in Snowflake Data Engineering

A stored procedure is a named, executable program stored in a database and invoked to perform data or control operations.

A stored procedure is a named Snowflake program that encapsulates data transformation or control logic and runs through a single procedure call. It can replace repeatedly issued SQL, while adding branching, looping, exception handling, and logging where the pipeline needs them. section 4.3 section 4.6

Use a procedure when a pipeline must merge staged data into a target table whenever new data arrives. A task or another orchestration mechanism can invoke that procedure, keeping the load operation centralized and repeatable. section 4.3 section 4.3.1

A basic Snowflake Scripting procedure defines:

  • A CREATE PROCEDURE declaration with a name and optional parameters. section 4.3.1
  • A RETURNS clause for the result type and a LANGUAGE clause; SQL denotes Snowflake Scripting. section 4.3.1
  • A body enclosed in $$, with BEGIN and END around the statements to execute. section 4.3.1
  • An optional RETURN statement that reports execution information, such as rows affected through SQLROWCOUNT. section 4.3.2

Loading combined customer orders

LOAD_CUSTOMER_ORDERS executes a MERGE from combined staging data into CUSTOMER_ORDERS_COMBINED. Calling it loads the target table; adding RETURN 'Load completed.' with SQLROWCOUNT reports the affected-row count after the merge. section 4.3.1 section 4.3.2

Procedures may be written in Snowflake Scripting, JavaScript, Java, Python, or Scala. For work requiring procedural retrieval of model output, transformation, and saving results, a Python procedure can accept an input parameter, use a handler function, and return a table. section 4.3 section 7.4.1

For resilient pipelines, add exception handling to catch errors or unexpected outcomes and perform remediation or notification. Built-in logging can capture procedure messages during execution in an event table, enabling periodic pipeline-status review. section 4.6 section 4.6

How Snowflake Data Engineering covers it

How deep it goes
Builds a Snowflake Scripting procedure from declaration through CALL and RETURN, then extends its role to exception handling and logging.
Examples
Demonstrates LOAD_CUSTOMER_ORDERS merging staged bakery orders and READ_EMAIL_PROC extracting structured order information from customer emails.
What it assumes
Introduces only the Snowflake Scripting syntax required to build the procedure, starting from an existing staging-to-target MERGE.
What you can do after
Apply it via Snowflake Scripting to centralize a MERGE load, invoke it with CALL, and return affected-row status.
How it connects
Connects stored procedures to MERGE, Snowflake Scripting, tasks, exception handling, logging, Snowpark, UDFs, and external network access.

Also explained in

Compare how 4 books explain stored procedure →

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.