September 16, 2024

What are Snowflake Stored Procedures?

Dexter Chu
Head of Marketing

Stored procedures are a crucial part of any database system, and Snowflake is no exception. In this guide, we will delve into the concept of Snowflake stored procedures, their purpose, supported languages, syntax, privileges, and practical usage.

Snowflake stored procedures are used to encapsulate SQL logic within procedural code, enabling the execution of complex database operations. They automate tasks that require multiple SQL statements, such as cleaning up a database by deleting old data, transforming data, or performing complex computations. This encapsulation enhances code reusability, maintainability, and execution efficiency.

What languages are supported for Snowflake Stored Procedures?

Snowflake offers support for stored procedures in multiple programming languages, allowing developers to choose the one that best fits their needs or their team's expertise. The supported languages include:

  • Java (using Snowpark)
  • JavaScript
  • Python (using Snowpark)
  • Scala (using Snowpark)
  • Snowflake Scripting

This flexibility ensures that various application requirements can be met effectively.

How is the syntax for creating Snowflake Stored Procedures structured?

The syntax for creating Snowflake stored procedures varies depending on the programming language used. Below is an overview of the syntax for different languages:

  • Java Handler: There are two types of handlers - In-line Handler Code and Precompiled Handler.
  • JavaScript Handler: There is a specific syntax for creating JavaScript handlers.
  • Python Handler: There are two types of handlers - In-line Handler Code and Handler Code in a File on a Stage.
  • Scala Handler: There are two types of handlers - In-line Handler Code and Precompiled Handler.
  • Snowflake Scripting Handler: There is a specific syntax for creating Snowflake Scripting handlers.

What are the privileges and execution types for Snowflake Stored Procedures?

Stored procedures in Snowflake can be executed with either Caller’s Rights or Owner’s Rights:

  • Caller’s Rights: The stored procedure uses the privileges of the caller. This allows the procedure to access and manipulate data that the caller has permissions for.
  • Owner’s Rights: The stored procedure uses the privileges of the owner (the user who created the procedure). This can be useful for delegating tasks without granting extensive privileges to the procedure caller.

How are Snowflake Stored Procedures used in practice?

Stored procedures in Snowflake are utilized for various database tasks. Here’s an example of creating and calling a Python stored procedure:

  • Creating a Python Procedure: There is a specific syntax for creating a Python procedure.
  • Calling the Procedure: There is a specific syntax for calling the created procedure.

What are some additional notes on Snowflake Stored Procedures?

There are a few additional points to note when working with Snowflake Stored Procedures:

  • VOLATILE and IMMUTABLE: These attributes are deprecated and should be avoided in new procedures.
  • Case Sensitivity: JavaScript is case-sensitive, while SQL is not.
  • Anonymous Procedures: Creating an anonymous procedure does not require a role with CREATE PROCEDURE schema privileges.

Recap of Snowflake Stored Procedures

Snowflake stored procedures are powerful tools that enable the encapsulation of complex SQL logic within procedural code. They support multiple programming languages, including Java, JavaScript, Python, Scala, and Snowflake Scripting, providing flexibility for developers. The ability to execute procedures with either caller’s rights or owner’s rights enhances security and flexibility in database operations. With Snowflake's robust syntax and support for various handler types, stored procedures can significantly streamline and automate database tasks, making them an invaluable asset for any data-driven organization.

  • Stored procedures encapsulate complex SQL logic within procedural code, enhancing code reusability, maintainability, and execution efficiency.
  • Snowflake supports multiple programming languages for stored procedures, providing flexibility for developers.
  • The syntax for creating Snowflake stored procedures varies depending on the programming language used.

Keep reading

View all