Working with stored procedures | Snowflake Documentation (original) (raw)

Stored procedures enable users to create modular code that can include complex business logic by combining multiple SQL statements with procedural logic.

Note

To both create and call an anonymous procedure, use CALL (with anonymous procedure). Creating and calling an anonymous procedure does not require a role with CREATE PROCEDURE schema privileges.

Naming conventions for stored procedures

You must name procedures according to conventions enforced by Snowflake.

For more information, see Naming and overloading procedures and UDFs.

Transaction management

Stored procedures are not atomic; if one statement in a stored procedure fails, the other statements in the stored procedure are not necessarily rolled back.

You can use stored procedures with transactions to make a group of statements atomic. For details, seeStored procedures and transactions.

General tips

Symmetric code

If you are familiar with programming in assembly language, you might find the following analogy helpful. In assembly language, functions often create and undo their environments in a symmetric way. For example:

-- Set up. push a; push b; ... -- Clean up in the reverse order that you set up. pop b; pop a;

You might want to use this approach in your stored procedures:

For example your code might look similar to the pseudo-code shown below:

CREATE PROCEDURE f() ... >setx;>sety;>try>setz;>−−Dosomethinginteresting...>...>unsetz;>>catch>−−Giveerrormessage...>...>unsetz;>>unsety;>unsetx;>> set x; > set y; > try { > set z; > -- Do something interesting... > ... > unset z; > } > catch { > -- Give error message... > ... > unset z; > } > unset y; > unset x; >>setx;>sety;>try>setz;>Dosomethinginteresting...>...>unsetz;>>catch>Giveerrormessage...>...>unsetz;>>unsety;>unsetx;> ;

Calling a stored procedure

You call a stored procedure using a SQL command. For more information on calling stored procedures, seeCalling a stored procedure.

Selecting from a stored procedure

You call a stored procedure in the FROM clause of a SELECT statement. For more information on selecting from a stored procedure, seeSelecting from a stored procedure.

Privileges

Stored Procedures utilize two types of privileges:

Privileges on stored procedures

Similar to other database objects (tables, views, UDFs, etc.), stored procedures are owned by a role and have one or more privileges that can be granted to other roles.

Currently, the following privileges apply to stored procedures:

For a role to use a stored procedure, the role must either be the owner or have been granted USAGE privilege on the stored procedure.

Privileges on the database objects accessed by the stored procedure

This subject is covered in Understanding caller’s rights and owner’s rights stored procedures.

Stored procedure considerations

Working with stored procedures in Snowsight

You can work with stored procedures in SQL or in Snowsight.

For any stored procedure in Snowflake, you can open Data » Databases and search for or browse to the stored procedure. Select the stored procedure to review details and manage the procedure.

You must have the relevant privileges to access and manage the stored procedure in Snowsight.

Explore stored procedure details in Snowsight

After opening a stored procedure in Snowsight, you can do the following:

Manage a stored procedure in Snowsight

You can perform the following basic management tasks for a stored procedure in Snowsight:

SQL injection

Stored procedures can dynamically create a SQL statement and execute it. However, this can allow SQL injection attacks, particularly if you create the SQL statement using input from a public or untrusted source.

You can minimize the risk of SQL injection attacks by binding parameters rather than concatenating text. For an example of binding variables, see Binding variables.

If you choose to use concatenation, you should check inputs carefully when constructing SQL dynamically using input from public sources. You might also want to take other precautions, such as querying using a role that has limited privileges (e.g. read-only access, or access to only certain tables or views).

For more information about SQL injection attacks, seeSQL injection (in Wikipedia).

Design tips for stored procedures

Here are some tips for designing a stored procedure:

Documenting stored procedures

Stored procedures are usually written to be re-used, and often to be shared. Documenting stored procedures can make stored procedures easier to use and easier to maintain.

Below are some general recommendations for documenting stored procedures.

Typically, there are at least two audiences who want to know about a stored procedure:

For users (and programmers), document each of the following:

For programmers:

The location and format of this information are up to you. You might store the information in HTML format in an internal web site, for example. Before deciding where to store it, think about where your organization stores similar information for other products, or similar information for other Snowflake features, such as views, user-defined functions, etc.

Other tips: