How to Create Stored Procedures in Snowflake

Galaxy Glossary

How do I create and use stored procedures in Snowflake?

Stored procedures in Snowflake let you bundle SQL statements and JavaScript logic into reusable, version-controlled database objects.

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 does a Snowflake stored procedure do?

It executes multiple SQL statements and JavaScript expressions as a single programmable unit, returning a value when finished. Use it for complex data transformations, auditing, or conditional logic.

Why choose a stored procedure over a view or script?

Procedures support variables, loops, error handling, and versioning, enabling advanced ETL logic that a view cannot perform and centralizing logic that ad-hoc scripts scatter.

How do I create a stored procedure?

Use the CREATE PROCEDURE command with a JavaScript handler.Define input parameters, return type, language (javascript or sql), and the executable body.

Example

CREATE OR REPLACE PROCEDURE process_daily_orders(proc_date DATE)
RETURNS STRING
LANGUAGE javascript
EXECUTE AS owner
AS $$
var sql_cmd = `
INSERT INTO Orders_Audit
SELECT * FROM Orders WHERE order_date = '` + proc_date + `';
`;
snowflake.execute({sqlText: sql_cmd});
return 'Audit complete for ' + proc_date;
$$;

How do I call a procedure?

Invoke with the CALL statement.Example: CALL process_daily_orders('2023-08-01');

How can I modify an existing procedure?

Use CREATE OR REPLACE PROCEDURE with the same name or use ALTER PROCEDURE … SET TAG for metadata updates.

Best practices for stored procedures

• Keep procedures single-purpose.
• Use EXECUTE AS OWNER to avoid permission errors.
• Log actions to an audit table.
• Version control scripts outside Snowflake.

Common use case: recalculating customer lifetime value (CLV)

Wrap a complex CLV computation that touches Customers, Orders, OrderItems, and Products in a single procedure so analysts can simply CALL it.

.

Why How to Create Stored Procedures in Snowflake is important

How to Create Stored Procedures in Snowflake Example Usage


-- Create a procedure that restocks low-inventory products
CREATE OR REPLACE PROCEDURE restock_products(min_stock INTEGER)
RETURNS INTEGER
LANGUAGE javascript
EXECUTE AS owner
AS $$
  var stmt = snowflake.createStatement({
    sqlText: `UPDATE Products SET stock = stock + 100 WHERE stock < ?`,
    binds: [MIN_STOCK]
  });
  var rows = stmt.execute();
  return rows.getRowCount();
$$;

-- Call it to restock items with fewer than 20 units
CALL restock_products(20);

How to Create Stored Procedures in Snowflake Syntax


CREATE [ OR REPLACE ] PROCEDURE <db>.<schema>.<name>
  ( [ param_name data_type [ , ... ] ] )
RETURNS <return_type>
LANGUAGE { javascript | sql }
[ EXECUTE AS { CALLER | OWNER } ]
AS $$
  // JavaScript or SQL code
$$;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I write procedures purely in SQL?

Yes. Use LANGUAGE SQL and wrap logic in BEGIN … END; blocks.

How do I debug a JavaScript procedure?

Insert snowflake.execute() calls to log intermediate results into a staging table, or RAISE_EXCEPTION to surface messages.

Want to learn about other SQL terms?

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