Stored procedures in Snowflake let you bundle SQL statements and JavaScript logic into reusable, version-controlled database objects.
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.
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.
Use the CREATE PROCEDURE command with a JavaScript handler.Define input parameters, return type, language (javascript or sql), and the executable body.
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;
$$;
Invoke with the CALL statement.Example: CALL process_daily_orders('2023-08-01');
Use CREATE OR REPLACE PROCEDURE with the same name or use ALTER PROCEDURE … SET TAG for metadata updates.
• Keep procedures single-purpose.
• Use EXECUTE AS OWNER
to avoid permission errors.
• Log actions to an audit table.
• Version control scripts outside Snowflake.
Wrap a complex CLV computation that touches Customers, Orders, OrderItems, and Products in a single procedure so analysts can simply CALL it.
.
Yes. Use LANGUAGE SQL and wrap logic in BEGIN … END; blocks.
Insert snowflake.execute()
calls to log intermediate results into a staging table, or RAISE_EXCEPTION to surface messages.