In this post we are going to cover stored procedures in Snowflake.
A function gives you a value inside a query. A procedure is a statement of its own, you run it with CALL, and it is allowed to change things.
We will start with the smallest procedure there is and build it up step by step. Along the way we will find that the RETURNS clause, which a SQL function quietly ignored, is enforced here.
Every query below was run on a real Snowflake account, and every result you see is the output it gave back.
Episode 145 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's create some sample data
- Let's start with the smallest procedure
- So is that really a result set?
- Now let's see what the catalog says about it
- Now the part that catches people: CALL is a statement
- But the argument is ordinary SQL
- Now the thing a function could not do
- Does DDL work in there too?
- Now the RETURNS clause, which is enforced this time
- What happens with no RETURN statement at all?
- Now let's return rows instead of a value
- Now transactions, and one thing I cannot explain
- Two more spellings of rollback
- What if a procedure opens a transaction and never closes it?
- Whose rights does a procedure run with?
- Can two procedures share a name?
- And a task whose whole body is a CALL
- One last thing: what a procedure refuses
Let's create some sample data
Procedures in this post read a table and write to it, so we need a table first. The script below creates SALES_T145 and puts three rows into it.
-- Sample data for this post.
-- Three sales rows for the procedures to read and change.
USE DATABASE DEMO_DB;
USE SCHEMA PUBLIC;
CREATE OR REPLACE TABLE SALES_T145 (
SALE_ID INT,
REGION VARCHAR(10),
AMOUNT NUMBER(10,2)
);
INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT) VALUES
(1, 'north', 100.00),
(2, 'south', 250.50),
(3, 'east', 75.25);INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT)
VALUES (1, 'north', 100.00), (2, 'south', 250.50), (3, 'east', 75.25);| number of rows inserted |
|---|
| 3 |
Let's note what is in there before anything touches it, because we are going to come back to this count twice.
SELECT COUNT(*) AS ROWS_IN_IT, SUM(AMOUNT) AS TOTAL_AMOUNT FROM SALES_T145;| ROWS_IN_IT | TOTAL_AMOUNT |
|---|---|
| 3 | 425.75 |
Three rows and a total. Now let's write a procedure.
Let's start with the smallest procedure
Here is one that doubles a number.
CREATE OR REPLACE PROCEDURE DOUBLE_T145(N FLOAT)
RETURNS FLOAT
LANGUAGE SQL
AS
$$
BEGIN
RETURN :N * 2;
END;
$$;| status |
|---|
| Function DOUBLE_T145 successfully created. |
Two things to notice in that body, and both of them are new. The body has to be wrapped in BEGIN and END. And the argument is used with a colon in front of it, as :N.
That colon is Snowflake Scripting rather than plain SQL, and it has a whole post of its own coming next. For now, write it.
Now let's run it. CALL, not SELECT.
CALL DOUBLE_T145(21);| DOUBLE_T145 |
|---|
| 42.0 |
One row and one column. And look at the column heading: it is named after the procedure.
So is that really a result set?
That heading named after the procedure is a hint. If a CALL produces a real result set, then RESULT_SCAN should be able to read it back.
LAST_QUERY_ID() means the statement we just ran, so this has to go immediately after that CALL and nothing may come between them.
SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| DOUBLE_T145 |
|---|
| 42.0 |
Same column, same name, same value. So yes, a CALL produces a one cell result set like any other query.
And we can ask for that one column by position rather than by name, which proves the name is just a name.
SELECT $1 AS THE_ONLY_COLUMN FROM TABLE(RESULT_SCAN(LAST_QUERY_ID(-2)));| THE_ONLY_COLUMN |
|---|
| 42.0 |
Now let's see what the catalog says about it
A procedure is an object in the schema, so SHOW lists it.
SHOW PROCEDURES LIKE '%\\_T145';| created_on | name | schema_name | is_builtin | is_aggregate | is_ansi | min_num_arguments | max_num_arguments | arguments | description | catalog_name | is_table_function | valid_for_clustering | is_secure | secrets | external_access_integrations |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2026-10-03 13:54:49.951000-07:00 | DOUBLE_T145 | PUBLIC | N | N | N | 1 | 1 | DOUBLE_T145(FLOAT) RETURN FLOAT | user-defined procedure | DEMO_DB | N | N | N |
The arguments column carries the signature and the return type in one string. The last two cells of that row are empty strings rather than nulls, which is how SHOW reports a property that has nothing in it.
DESCRIBE PROCEDURE gives the same information the other way up, one property per row.
DESCRIBE PROCEDURE DOUBLE_T145(FLOAT);| property | value |
|---|---|
| signature | (N FLOAT) |
| returns | FLOAT |
| language | SQL |
| execute as | OWNER |
| body | BEGIN RETURN :N * 2; END; |
Signature, returns, language, the body, and execute as, which we will come back to near the end of this post.
And the catalog view has its own column for each of those, which is easier to query against.
SELECT PROCEDURE_NAME,
ARGUMENT_SIGNATURE,
DATA_TYPE,
PROCEDURE_LANGUAGE
FROM INFORMATION_SCHEMA.PROCEDURES
WHERE PROCEDURE_SCHEMA = 'PUBLIC'
AND ENDSWITH(PROCEDURE_NAME, '_T145')
ORDER BY PROCEDURE_NAME;| PROCEDURE_NAME | ARGUMENT_SIGNATURE | DATA_TYPE | PROCEDURE_LANGUAGE |
|---|---|---|---|
| DOUBLE_T145 | (N FLOAT) | FLOAT | SQL |
Now the part that catches people: CALL is a statement
A procedure is not a function, and the clearest way to see that is to try to use it like one. First, in a SELECT list.
SELECT DOUBLE_T145(21) AS IN_A_SELECT_LIST;002139 (02000): SQL compilation error: Unknown function DOUBLE_T145.
002139, unknown function. It is not a function, so as far as a SELECT list is concerned it does not exist.
Now in a FROM clause.
SELECT * FROM TABLE(DOUBLE_T145(21));002142 (42601): SQL compilation error: Unknown table function DOUBLE_T145
002142, unknown table function. A different code for a different mistake, which is helpful.
And two calls in one statement.
CALL DOUBLE_T145(1), DOUBLE_T145(2);001003 (42000): SQL compilation error: syntax error line 1 at position 19 unexpected ','.
001003, syntax error at the comma. One CALL is one statement.
But the argument is ordinary SQL
The CALL itself is rigid. What goes inside the brackets is not. Let's set a session variable and pass it.
SET THE_INPUT = 50;| status |
|---|
| Statement executed successfully. |
CALL DOUBLE_T145($THE_INPUT);| DOUBLE_T145 |
|---|
| 100.0 |
It took the variable. And an expression works too.
CALL DOUBLE_T145(3 * 7);| DOUBLE_T145 |
|---|
| 42.0 |
So the statement is fixed and the argument is normal. That is a useful split to keep in your head.
Now the thing a function could not do
A SQL function cannot contain an INSERT. Let's write exactly that and watch it be refused again, so the comparison is on this page rather than in another post.
CREATE OR REPLACE FUNCTION ADDROW_T145(ID INT, A NUMBER(10,2))
RETURNS INT
AS
$$
INSERT INTO SALES_T145 (SALE_ID, AMOUNT) VALUES (ID, A)
$$;090203 (42601): Compilation of SQL UDF failed: SQL compilation error: syntax error line 2 at position 11 unexpected 'INTO'. syntax error line 2 at position 28 unexpected 'SALE_ID'. syntax error line 2 at position 45 unexpected 'VALUES'.
090203, a syntax error at INTO. A function body is an expression, and an expression cannot contain a statement.
Now the same INSERT as a procedure.
CREATE OR REPLACE PROCEDURE ADDROW_T145(ID INT, A NUMBER(10,2))
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT)
VALUES (:ID, 'added', :A);
RETURN 'inserted ' || :ID;
END;
$$;| status |
|---|
| Function ADDROW_T145 successfully created. |
Created without a complaint. Let's call it.
CALL ADDROW_T145(4, 310.00);| ADDROW_T145 |
|---|
| inserted 4 |
So let's look at the table.
SELECT SALE_ID, REGION, AMOUNT FROM SALES_T145 ORDER BY SALE_ID;| SALE_ID | REGION | AMOUNT |
|---|---|---|
| 1 | north | 100.00 |
| 2 | south | 250.50 |
| 3 | east | 75.25 |
| 4 | added | 310.00 |
The new row is there, with the region the procedure body put in it rather than one we passed. And the caller sees the change immediately.
SELECT COUNT(*) AS ROWS_IN_IT, SUM(AMOUNT) AS TOTAL_AMOUNT FROM SALES_T145;| ROWS_IN_IT | TOTAL_AMOUNT |
|---|---|
| 4 | 735.75 |
Compare that with the count we took in the first section. There is nothing to commit and nothing to wait for.
Does DDL work in there too?
It does. Here is a procedure that creates a table and fills it.
CREATE OR REPLACE PROCEDURE MAKEATABLE_T145(THE_NAME VARCHAR)
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
CREATE OR REPLACE TABLE MADE_BY_PROCEDURE_T145 (NOTE VARCHAR(40));
INSERT INTO MADE_BY_PROCEDURE_T145 (NOTE) VALUES (:THE_NAME);
RETURN 'made it';
END;
$$;| status |
|---|
| Function MAKEATABLE_T145 successfully created. |
CALL MAKEATABLE_T145('made inside a procedure');| MAKEATABLE_T145 |
|---|
| made it |
And the table it made is there afterwards, with its row in it.
SELECT NOTE FROM MADE_BY_PROCEDURE_T145;| NOTE |
|---|
| made inside a procedure |
Now the RETURNS clause, which is enforced this time
Here is a procedure that declares RETURNS INT and then returns a string. Watch when Snowflake objects.
CREATE OR REPLACE PROCEDURE LIES_T145()
RETURNS INT
LANGUAGE SQL
AS
$$
BEGIN
RETURN 'not a number';
END;
$$;| status |
|---|
| Function LIES_T145 successfully created. |
Created, with no complaint at all. Now let's call it.
CALL LIES_T145();100038 (22018): Uncaught exception of type 'EXPRESSION_ERROR' on line 3 at position 11 : Numeric value 'not a number' is not recognized
100038, an expression error, because that string is not a number.
This is worth sitting with. A SQL function declared VARCHAR(5) will hand you back ten characters and say nothing. A procedure's RETURNS clause is checked when the RETURN runs.
A function's RETURNS clause behaves like metadata. A procedure's behaves like a declaration. Same keyword, two different levels of seriousness.
And the same thing in the quieter direction
That was the loud version. Here is the one you would never notice. A function that adds tax, declared NUMBER(12,4).
CREATE OR REPLACE FUNCTION WITH_TAX_T145(A NUMBER(10,2))
RETURNS NUMBER(12,4)
AS
$$
A * 1.20
$$;| status |
|---|
| Function WITH_TAX_T145 successfully created. |
And a procedure that sums it up, also declared NUMBER(12,4).
CREATE OR REPLACE PROCEDURE TAXEDTOTAL_T145()
RETURNS NUMBER(12,4)
LANGUAGE SQL
AS
$$
DECLARE
THE_TOTAL NUMBER(12,4);
BEGIN
SELECT SUM(WITH_TAX_T145(AMOUNT)) INTO :THE_TOTAL FROM SALES_T145;
RETURN :THE_TOTAL;
END;
$$;| status |
|---|
| Function TAXEDTOTAL_T145 successfully created. |
CALL TAXEDTOTAL_T145();| TAXEDTOTAL_T145 |
|---|
| 884.1000 |
Now the same sum run straight in the worksheet, with no procedure anywhere.
SELECT SUM(WITH_TAX_T145(AMOUNT)) AS THE_SAME_TOTAL FROM SALES_T145;| THE_SAME_TOTAL |
|---|
| 884.100 |
The same total, written to a different number of decimal places. The procedure applied the scale it declared. The worksheet query had nothing to apply.
What happens with no RETURN statement at all?
Here is a procedure whose body only inserts a row. There is no RETURN in it.
CREATE OR REPLACE PROCEDURE NORETURN_T145()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT) VALUES (98, 'quiet', 1.00);
END;
$$;| status |
|---|
| Function NORETURN_T145 successfully created. |
CALL NORETURN_T145();| NORETURN_T145 |
|---|
| NULL |
A null. But the work still happened, and we can check.
SELECT COUNT(*) AS ROWS_IN_IT FROM SALES_T145 WHERE SALE_ID = 98;| ROWS_IN_IT |
|---|
| 1 |
The row the body inserted is there. A null answer does not mean nothing happened.
And can a procedure call another procedure?
Yes, and reading its answer back is the RESULT_SCAN trick from earlier, used inside a body.
CREATE OR REPLACE PROCEDURE OUTER_T145(N FLOAT)
RETURNS FLOAT
LANGUAGE SQL
AS
$$
DECLARE
THE_ANSWER FLOAT;
BEGIN
CALL DOUBLE_T145(:N);
SELECT DOUBLE_T145 INTO :THE_ANSWER
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));
RETURN :THE_ANSWER + 1;
END;
$$;| status |
|---|
| Function OUTER_T145 successfully created. |
CALL OUTER_T145(10);| OUTER_T145 |
|---|
| 21.0 |
The inner procedure doubled the number, the outer one read that answer out of the inner call's result set and added one.
Now let's return rows instead of a value
A procedure can declare RETURNS TABLE, put a query into a RESULTSET, and hand that back with RETURN TABLE.
Here is mine, and this is the point where my own mistake is on the page, because the capture is the capture.
CREATE OR REPLACE PROCEDURE BIGSALES_T145(CUTOFF NUMBER(10,2))
RETURNS TABLE (SALE_ID INT, THE_AMOUNT NUMBER(10,2))
LANGUAGE SQL
AS
$$
DECLARE
RS RESULTSET DEFAULT (
SELECT SALE_ID, AMOUNT FROM SALES_T145 WHERE AMOUNT > ?
);
BEGIN
RS := (SELECT SALE_ID, AMOUNT FROM SALES_T145 WHERE AMOUNT > :CUTOFF);
RETURN TABLE(RS);
END;
$$;| status |
|---|
| Function BIGSALES_T145 successfully created. |
Created happily. Now let's call it.
CALL BIGSALES_T145(80.00);092240 (P0000): SQL compilation error: error line 4 at position 62
Unexpected unnamed bind in {2}.092240, unexpected unnamed bind. Look at the DEFAULT clause on the RESULTSET declaration: there is a question mark in it. I typed that question mark while drafting the query, meaning to bind it later, and never took it out.
And it is dead code. The very next line assigns the RESULTSET properly. It still took the whole procedure down.
The RESULT_SCAN I ran after that failed call had nothing to read, because the query it pointed at had failed. That error quotes a query id, so it is not on this page.
The same procedure with that line deleted
I re-ran this part on its own, on a fresh table with four rows.
CREATE OR REPLACE TABLE SALES_T145B (
SALE_ID INT,
REGION VARCHAR(16),
AMOUNT NUMBER(10,2)
);| status |
|---|
| Table SALES_T145B successfully created. |
INSERT INTO SALES_T145B (SALE_ID, REGION, AMOUNT)
VALUES (1, 'north', 100.00), (2, 'south', 250.50),
(3, 'east', 75.25), (4, 'west', 310.00);| number of rows inserted |
|---|
| 4 |
And here is the procedure with the dead DEFAULT line removed.
CREATE OR REPLACE PROCEDURE BIGSALES_T145B(CUTOFF NUMBER(10,2))
RETURNS TABLE (SALE_ID INT, THE_AMOUNT NUMBER(10,2))
LANGUAGE SQL
AS
$$
DECLARE
RS RESULTSET;
BEGIN
RS := (SELECT SALE_ID, AMOUNT FROM SALES_T145B WHERE AMOUNT > :CUTOFF ORDER BY SALE_ID);
RETURN TABLE(RS);
END;
$$;| status |
|---|
| Function BIGSALES_T145B successfully created. |
Now the question the section was really for. Can the next statement read the rows the procedure handed back?
So let's call it, and then ask the very next statement for those rows.
CALL BIGSALES_T145B(80.00);SELECT SALE_ID, THE_AMOUNT
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
ORDER BY SALE_ID;| SALE_ID | THE_AMOUNT |
|---|---|
| 1 | 100.00 |
| 2 | 250.50 |
| 4 | 310.00 |
Three rows over the cutoff, read by a separate statement after the call. So a procedure really can hand a table to its caller.
Have a look at the second column heading there. It says THE_AMOUNT, which is the name the RETURNS TABLE clause declared. The table it came from calls that column something else.
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'SALES_T145B'
ORDER BY ORDINAL_POSITION;| COLUMN_NAME | DATA_TYPE |
|---|---|
| SALE_ID | NUMBER |
| REGION | TEXT |
| AMOUNT | NUMBER |
The table's own columns are listed there, and the amount column is plain AMOUNT. The procedure renamed it on the way out.
And a RESULTSET with a real default in it
I do not want to leave anyone thinking DEFAULT is the problem, because it is not. Here is one whose default is a complete query.
CREATE OR REPLACE PROCEDURE WITHDEFAULT_T145B()
RETURNS TABLE (SALE_ID INT, THE_AMOUNT NUMBER(10,2))
LANGUAGE SQL
AS
$$
DECLARE
RS RESULTSET DEFAULT (SELECT SALE_ID, AMOUNT FROM SALES_T145B ORDER BY SALE_ID);
BEGIN
RETURN TABLE(RS);
END;
$$;| status |
|---|
| Function WITHDEFAULT_T145B successfully created. |
CALL WITHDEFAULT_T145B();| SALE_ID | THE_AMOUNT |
|---|---|
| 1 | 100.00 |
| 2 | 250.50 |
| 3 | 75.25 |
| 4 | 310.00 |
All four rows, straight out of the CALL. The DEFAULT clause was fine. My question mark was not.
Now transactions, and one thing I cannot explain
Let's take the row count before any of this, so we have something to compare against.
SELECT COUNT(*) AS ROWS_BEFORE FROM SALES_T145;| ROWS_BEFORE |
|---|
| 5 |
Here is a procedure that begins a transaction, inserts a row, and commits.
CREATE OR REPLACE PROCEDURE COMMITS_T145()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
BEGIN TRANSACTION;
INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT) VALUES (50, 'kept', 5.00);
COMMIT;
RETURN 'committed';
END;
$$;| status |
|---|
| Function COMMITS_T145 successfully created. |
CALL COMMITS_T145();| COMMITS_T145 |
|---|
| committed |
Straightforward. Now the same shape with ROLLBACK instead of COMMIT.
CREATE OR REPLACE PROCEDURE ROLLSBACK_T145()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
BEGIN TRANSACTION;
INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT) VALUES (51, 'thrown away', 5.00);
ROLLBACK;
RETURN 'rolled back';
END;
$$;| status |
|---|
| Function ROLLSBACK_T145 successfully created. |
CALL ROLLSBACK_T145();090232 (25000): Stored procedure execution error: Scoped transaction started in stored procedure is incomplete and it was rolled back.
090232: the scoped transaction started in the stored procedure was incomplete, and it was rolled back.
Let's see which of the two rows those procedures tried to write is actually in the table.
SELECT SALE_ID, REGION FROM SALES_T145 WHERE SALE_ID IN (50, 51) ORDER BY SALE_ID;| SALE_ID | REGION |
|---|---|
| 50 | kept |
One row, the committed one. And nothing was left open.
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION;| THE_OPEN_TRANSACTION |
|---|
| NULL |
That is a real NULL in that cell, which is what CURRENT_TRANSACTION() returns when there is no transaction open.
And then the same code behaved differently
I ran that same rollback shape again in a later pass, on a fresh table, under a different name.
CREATE OR REPLACE PROCEDURE ROLLBACK1_T145B()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
BEGIN TRANSACTION;
INSERT INTO SALES_T145B (SALE_ID, REGION, AMOUNT) VALUES (51, 'plain', 5.00);
ROLLBACK;
RETURN 'rolled back';
END;
$$;| status |
|---|
| Function ROLLBACK1_T145B successfully created. |
CALL ROLLBACK1_T145B();| ROLLBACK1_T145B |
|---|
| rolled back |
No error at all. The same three statements in the same order.
I am going to say the honest thing here rather than invent a reason. I do not know what differed between those two runs.
What both of them agree on is the part that matters: the inserted row was not kept, and no transaction was left open afterwards.
Two more spellings of rollback
While we are on that fresh table, here is ROLLBACK TRANSACTION, written out the way you would write it in a worksheet.
CREATE OR REPLACE PROCEDURE ROLLBACK2_T145B()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
BEGIN TRANSACTION;
INSERT INTO SALES_T145B (SALE_ID, REGION, AMOUNT) VALUES (52, 'spelled out', 5.00);
ROLLBACK TRANSACTION;
RETURN 'rolled back';
END;
$$;001003 (42000): SQL compilation error: syntax error line 5 at position 13 unexpected 'TRANSACTION'. syntax error line 6 at position 4 unexpected 'RETURN'.
001003, syntax error at TRANSACTION. Inside a Scripting block it is plain ROLLBACK, and the procedure was never created.
CALL ROLLBACK2_T145B();002139 (02000): SQL compilation error: Unknown function ROLLBACK2_T145B.
Which confirms it. Now the spelling I would actually write: roll back from inside an exception handler.
CREATE OR REPLACE PROCEDURE ROLLBACK3_T145B()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
BEGIN TRANSACTION;
INSERT INTO SALES_T145B (SALE_ID, REGION, AMOUNT) VALUES (53, 'via handler', 5.00);
RAISE STATEMENT_ERROR;
RETURN 'never reached';
EXCEPTION
WHEN STATEMENT_ERROR THEN
ROLLBACK;
RETURN 'rolled back in the handler';
END;
$$;| status |
|---|
| Function ROLLBACK3_T145B successfully created. |
CALL ROLLBACK3_T145B();| ROLLBACK3_T145B |
|---|
| rolled back in the handler |
That one ran, and the handler is where the roll back belongs. Let's see what survived all of that.
SELECT SALE_ID, REGION FROM SALES_T145B
WHERE SALE_ID IN (50, 51, 52, 53)
ORDER BY SALE_ID;| SALE_ID | REGION |
|---|---|
| 50 | committed |
One row, and the region on it says which procedure wrote it: the one that committed. Everything the rolling back procedures inserted is gone.
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION;| THE_OPEN_TRANSACTION |
|---|
| NULL |
What if a procedure opens a transaction and never closes it?
Let's find out, because this is the one you will do by accident.
CREATE OR REPLACE PROCEDURE LEAVESITOPEN_T145()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
BEGIN TRANSACTION;
INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT) VALUES (52, 'left open', 5.00);
RETURN 'I did not commit';
END;
$$;| status |
|---|
| Function LEAVESITOPEN_T145 successfully created. |
CALL LEAVESITOPEN_T145();090232 (25000): Stored procedure execution error: Scoped transaction started in stored procedure is incomplete and it was rolled back.
The same 090232 as before. So what is the state of the session afterwards?
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION;| THE_OPEN_TRANSACTION |
|---|
| NULL |
SELECT COUNT(*) AS ROWS_IN_IT FROM SALES_T145 WHERE SALE_ID = 52;| ROWS_IN_IT |
|---|
| 0 |
Nothing open, and the row is gone. Snowflake cleaned up after the procedure rather than leaving the session holding a transaction nobody can see.
I rolled back anyway, which costs nothing and is a good habit after a section like this one.
ROLLBACK;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION,
(SELECT COUNT(*) FROM SALES_T145 WHERE SALE_ID = 52) AS THE_ROW;| THE_OPEN_TRANSACTION | THE_ROW |
|---|---|
| NULL | 0 |
Whose rights does a procedure run with?
There are two clauses for this, and here is one procedure of each kind. Both bodies report the role and the user they are running as.
CREATE OR REPLACE PROCEDURE ASCALLER_T145()
RETURNS VARCHAR
LANGUAGE SQL
EXECUTE AS CALLER
AS
$$
BEGIN
RETURN 'running as ' || CURRENT_ROLE() || ', user ' || CURRENT_USER();
END;
$$;| status |
|---|
| Function ASCALLER_T145 successfully created. |
CREATE OR REPLACE PROCEDURE ASOWNER_T145()
RETURNS VARCHAR
LANGUAGE SQL
EXECUTE AS OWNER
AS
$$
BEGIN
RETURN 'running as ' || CURRENT_ROLE() || ', user ' || CURRENT_USER();
END;
$$;| status |
|---|
| Function ASOWNER_T145 successfully created. |
I am leaving the two answers off this page. They both carry the account's user name in the value, and this blog does not publish account details.
There is a better reason to leave them out as well. The account these were captured on has one role, so those two answers could not have differed from each other. A grid that cannot show a difference is not evidence about the difference, and I am not going to present it as if it were.
What the catalog shows about the pair is on the page, and it is worth seeing what it does not carry.
SHOW PROCEDURES LIKE 'AS%\\_T145';| created_on | name | schema_name | is_builtin | is_aggregate | is_ansi | min_num_arguments | max_num_arguments | arguments | description | catalog_name | is_table_function | valid_for_clustering | is_secure | secrets | external_access_integrations |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2026-10-03 13:55:07.316000-07:00 | ASCALLER_T145 | PUBLIC | N | N | N | 0 | 0 | ASCALLER_T145() RETURN VARCHAR | user-defined procedure | DEMO_DB | N | N | N | ||
| 2026-10-03 13:55:07.468000-07:00 | ASOWNER_T145 | PUBLIC | N | N | N | 0 | 0 | ASOWNER_T145() RETURN VARCHAR | user-defined procedure | DEMO_DB | N | N | N |
Two procedures, and nothing in that listing says which is which. The empty cells at the end of each row are empty strings, not nulls.
For the default we can ask a different question. Here is a plain procedure that returns a greeting and never said either clause.
CREATE OR REPLACE PROCEDURE SAYHELLO_T145()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
RETURN 'hello from a procedure';
END;
$$;| status |
|---|
| Function SAYHELLO_T145 successfully created. |
CALL SAYHELLO_T145();| SAYHELLO_T145 |
|---|
| hello from a procedure |
Nothing about rights anywhere in it. Now let's ask Snowflake to write that procedure out for us.
SELECT GET_DDL('PROCEDURE', 'SAYHELLO_T145()') AS THE_DEFAULT_DDL;| THE_DEFAULT_DDL |
|---|
| CREATE OR REPLACE PROCEDURE "SAYHELLO_T145"() RETURNS VARCHAR LANGUAGE SQL EXECUTE AS OWNER AS ' BEGIN RETURN ''hello from a procedure''; END; '; |
Snowflake wrote EXECUTE AS OWNER into the definition for us, so that is the default. Notice also that the body came back wrapped in single quotes rather than dollar signs, with the quotes inside it doubled.
Can two procedures share a name?
They can, as long as their arguments differ, exactly as functions do. Here is one that takes a number and one that takes a string.
CREATE OR REPLACE PROCEDURE LABEL_T145(N FLOAT)
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
RETURN 'a number: ' || :N;
END;
$$;| status |
|---|
| Function LABEL_T145 successfully created. |
CREATE OR REPLACE PROCEDURE LABEL_T145(S VARCHAR)
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
RETURN 'a string: ' || :S;
END;
$$;| status |
|---|
| Function LABEL_T145 successfully created. |
CALL LABEL_T145(7);| LABEL_T145 |
|---|
| a number: 7 |
CALL LABEL_T145('xyz');| LABEL_T145 |
|---|
| a string: xyz |
Each call went to the body that matches what we passed. And the catalog lists them as two separate objects with one name.
SHOW PROCEDURES LIKE 'LABEL\\_T145';| created_on | name | schema_name | is_builtin | is_aggregate | is_ansi | min_num_arguments | max_num_arguments | arguments | description | catalog_name | is_table_function | valid_for_clustering | is_secure | secrets | external_access_integrations |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 2026-10-03 13:55:08.291000-07:00 | LABEL_T145 | PUBLIC | N | N | N | 1 | 1 | LABEL_T145(FLOAT) RETURN VARCHAR | user-defined procedure | DEMO_DB | N | N | N | ||
| 2026-10-03 13:55:08.425000-07:00 | LABEL_T145 | PUBLIC | N | N | N | 1 | 1 | LABEL_T145(VARCHAR) RETURN VARCHAR | user-defined procedure | DEMO_DB | N | N | N |
Which means a DESCRIBE that does not say which overload it means cannot be answered.
DESCRIBE PROCEDURE LABEL_T145();002003 (02000): SQL compilation error: Procedure 'DEMO_DB.PUBLIC.LABEL_T145' does not exist or not authorized. Your primary role ACCOUNTADMIN or one of your secondary roles must have at least one privilege granted on PROCEDURE DEMO_DB.PUBLIC.LABEL_T145.
Have a look at how that refusal is worded. Snowflake does not say the name is ambiguous. It says a procedure with that exact signature, taking no arguments, does not exist, which is quite true.
And a task whose whole body is a CALL
A procedure is a statement, and a task runs a statement, so a task can run a procedure on a schedule.
CREATE OR REPLACE TASK CALLER_T145
WAREHOUSE = COMPUTE_WH
SCHEDULE = '1 MINUTE'
AS
CALL ADDROW_T145(60, 60.00);| status |
|---|
| Task CALLER_T145 successfully created. |
The SHOW TASKS listing for it is not on this page. It carries the account's user name in its own columns, and nothing in the rest of it is worth that.
Let's run the task by hand instead, and then immediately check whether the row the procedure inserts has arrived.
EXECUTE TASK CALLER_T145;| status |
|---|
| Task CALLER_T145 is scheduled to run immediately. |
SELECT COUNT(*) AS ROWS_IN_IT FROM SALES_T145 WHERE SALE_ID = 60;| ROWS_IN_IT |
|---|
| 0 |
Nothing. Read the message EXECUTE TASK gave back: it says the task is scheduled to run. It does not say it has run.
So the next statement looked before the task did anything. That is the thing to know about EXECUTE TASK, and it is an easy half hour to lose.
ALTER TASK IF EXISTS CALLER_T145 SUSPEND;| status |
|---|
| Statement executed successfully. |
One last thing: what a procedure refuses
A few quick ones to finish. First, no RETURNS clause at all.
CREATE OR REPLACE PROCEDURE NORETURNS_T145()
LANGUAGE SQL
AS
$$
BEGIN
RETURN 1;
END;
$$;092226 (42601): SQL compilation error: stored procedure definition is missing return type
092226. A procedure has to say what it returns, even when the answer is nothing useful.
Next, a body with no BEGIN and END around it.
CREATE OR REPLACE PROCEDURE NOBLOCK_T145()
RETURNS INT
LANGUAGE SQL
AS
$$
RETURN 1;
$$;001003 (42000): SQL compilation error: syntax error line 2 at position 4 unexpected 'RETURN'.
001003. The block is not decoration.
Now two calls that cannot work. Too many arguments, and then a name that does not exist.
CALL DOUBLE_T145(1, 2);000939 (22023): SQL compilation error: error line 0 at position -1 too many arguments for function [DOUBLE_T145(1, 2)] expected 1, got 2
CALL NO_SUCH_PROCEDURE_T145(1);002139 (02000): SQL compilation error: Unknown function NO_SUCH_PROCEDURE_T145.
And last, a procedure that reads a table which is not there. This one is created without a word of complaint.
CREATE OR REPLACE PROCEDURE NOTABLE_T145()
RETURNS INT
LANGUAGE SQL
AS
$$
DECLARE
N INT;
BEGIN
SELECT COUNT(*) INTO :N FROM NO_SUCH_TABLE_T145;
RETURN :N;
END;
$$;| status |
|---|
| Function NOTABLE_T145 successfully created. |
CALL NOTABLE_T145();002003 (42S02): Uncaught exception of type 'STATEMENT_ERROR' on line 5 at position 4 : SQL compilation error: Object 'NO_SUCH_TABLE_T145' does not exist or not authorized. This executable runs with owner's rights. The owner role ACCOUNTADMIN must have at least one privilege granted on TABLE NO_SUCH_TABLE_T145.
002003 at call time, and read the rest of that message. It tells us the procedure runs with the owner's rights, which is the default we found with GET_DDL a moment ago. The name inside the body is resolved when the body runs, not when it is written.
Key points
- A procedure body is wrapped in
BEGINandEND, and an argument is used with a colon in front of it. - You run a procedure with
CALL, and the result is a real result set whose column is named after the procedure. CALLcannot go in aSELECTlist (002139) or aFROMclause (002142), and two of them cannot share one statement.- The argument, though, is ordinary SQL. A session variable or an expression is fine.
- An
INSERTa function refused works in a procedure, and the caller sees the change at once. - DDL works in a procedure body too.
- A procedure's
RETURNSclause is enforced when theRETURNruns. A function's is not. - A declared scale is applied, which is the quiet version of the same thing.
- No
RETURNstatement gives a null, and the work inside the body still happens. RETURNS TABLEwith aRESULTSEThands rows back, and the next statement can read them withRESULT_SCAN.- A leftover
?in aDEFAULTclause is092240, even when the line is dead code. COMMITinside a procedure works. A plainROLLBACKerrored in one run and worked in another, and neither run kept the row or left a transaction open.ROLLBACK TRANSACTIONspelled out is a syntax error inside a block. Rolling back from anEXCEPTIONhandler is the shape to write.- A procedure that opens a transaction and never closes it is
090232, and Snowflake cleans up after it. EXECUTE AS OWNERis the default, and the name inside the body is resolved at run time with the owner's rights.- Procedures overload by argument type, so a
DESCRIBEhas to say which one it means. EXECUTE TASKschedules a run. The next statement will very likely look before anything has happened.
In the next post we slow down on the part of this one that was quietly doing all the work: Snowflake Scripting variables. DECLARE, LET, SELECT ... INTO, and the one place where that colon is not optional.
Every query and every result grid on this page was captured from a live Snowflake account (version 10.35.101) running the demo script for this episode. The table returning procedure and the rollback comparison were captured in a second pass, on their own table, which is why those names end in B. Three grids are deliberately not shown because they carry the account's user name or a query id, and the post says so where they would have been. Every count in the text is read off one of the grids on this page. Nothing is timed and no performance claims are made anywhere in this series. Behaviour follows the Snowflake CREATE PROCEDURE documentation.



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.