Snowflake SQL Tutorial 145 - The RETURNS Clause My Function Ignored Is Enforced In A Procedure

Snowflake SQL · Episode 145

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

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
-- 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);
The insert
INSERT INTO SALES_T145 (SALE_ID, REGION, AMOUNT)
VALUES (1, 'north', 100.00), (2, 'south', 250.50), (3, 'east', 75.25);
Result1 row · 1 column
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.

Query
SELECT COUNT(*) AS ROWS_IN_IT, SUM(AMOUNT) AS TOTAL_AMOUNT FROM SALES_T145;
Result1 row · 2 columns
ROWS_IN_ITTOTAL_AMOUNT
3425.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
CREATE OR REPLACE PROCEDURE DOUBLE_T145(N FLOAT)
RETURNS FLOAT
LANGUAGE SQL
AS
$$
BEGIN
    RETURN :N * 2;
END;
$$;
Result1 row · 1 column
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
CALL DOUBLE_T145(21);
Result1 row · 1 column
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.

Query
SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));
Result1 row · 1 column
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.

Query
SELECT $1 AS THE_ONLY_COLUMN FROM TABLE(RESULT_SCAN(LAST_QUERY_ID(-2)));
Result1 row · 1 column
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.

Query
SHOW PROCEDURES LIKE '%\\_T145';
Result1 row · 16 columns · scroll →
created_onnameschema_nameis_builtinis_aggregateis_ansimin_num_argumentsmax_num_argumentsargumentsdescriptioncatalog_nameis_table_functionvalid_for_clusteringis_securesecretsexternal_access_integrations
2026-10-03 13:54:49.951000-07:00DOUBLE_T145PUBLICNNN11DOUBLE_T145(FLOAT) RETURN FLOATuser-defined procedureDEMO_DBNNN

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.

Query
DESCRIBE PROCEDURE DOUBLE_T145(FLOAT);
Result5 rows · 2 columns
propertyvalue
signature(N FLOAT)
returnsFLOAT
languageSQL
execute asOWNER
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.

Query
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;
Result1 row · 4 columns
PROCEDURE_NAMEARGUMENT_SIGNATUREDATA_TYPEPROCEDURE_LANGUAGE
DOUBLE_T145(N FLOAT)FLOATSQL

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.

Query
SELECT DOUBLE_T145(21) AS IN_A_SELECT_LIST;
Query failed — Snowflake returned an error
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.

Query
SELECT * FROM TABLE(DOUBLE_T145(21));
Query failed — Snowflake returned an error
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.

Query
CALL DOUBLE_T145(1), DOUBLE_T145(2);
Query failed — Snowflake returned an error
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
SET THE_INPUT = 50;
Result1 row · 1 column
status
Statement executed successfully.
Call
CALL DOUBLE_T145($THE_INPUT);
Result1 row · 1 column
DOUBLE_T145
100.0

It took the variable. And an expression works too.

Call
CALL DOUBLE_T145(3 * 7);
Result1 row · 1 column
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
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)
$$;
Query failed — Snowflake returned an error
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
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;
$$;
Result1 row · 1 column
status
Function ADDROW_T145 successfully created.

Created without a complaint. Let's call it.

Call
CALL ADDROW_T145(4, 310.00);
Result1 row · 1 column
ADDROW_T145
inserted 4

So let's look at the table.

Query
SELECT SALE_ID, REGION, AMOUNT FROM SALES_T145 ORDER BY SALE_ID;
Result4 rows · 3 columns
SALE_IDREGIONAMOUNT
1north100.00
2south250.50
3east75.25
4added310.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.

Query
SELECT COUNT(*) AS ROWS_IN_IT, SUM(AMOUNT) AS TOTAL_AMOUNT FROM SALES_T145;
Result1 row · 2 columns
ROWS_IN_ITTOTAL_AMOUNT
4735.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
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;
$$;
Result1 row · 1 column
status
Function MAKEATABLE_T145 successfully created.
Call
CALL MAKEATABLE_T145('made inside a procedure');
Result1 row · 1 column
MAKEATABLE_T145
made it

And the table it made is there afterwards, with its row in it.

Query
SELECT NOTE FROM MADE_BY_PROCEDURE_T145;
Result1 row · 1 column
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
CREATE OR REPLACE PROCEDURE LIES_T145()
RETURNS INT
LANGUAGE SQL
AS
$$
BEGIN
    RETURN 'not a number';
END;
$$;
Result1 row · 1 column
status
Function LIES_T145 successfully created.

Created, with no complaint at all. Now let's call it.

Call
CALL LIES_T145();
Query failed — Snowflake returned an error
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.

The difference in one line

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
CREATE OR REPLACE FUNCTION WITH_TAX_T145(A NUMBER(10,2))
RETURNS NUMBER(12,4)
AS
$$
    A * 1.20
$$;
Result1 row · 1 column
status
Function WITH_TAX_T145 successfully created.

And a procedure that sums it up, also declared NUMBER(12,4).

Create
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;
$$;
Result1 row · 1 column
status
Function TAXEDTOTAL_T145 successfully created.
Call
CALL TAXEDTOTAL_T145();
Result1 row · 1 column
TAXEDTOTAL_T145
884.1000

Now the same sum run straight in the worksheet, with no procedure anywhere.

Query
SELECT SUM(WITH_TAX_T145(AMOUNT)) AS THE_SAME_TOTAL FROM SALES_T145;
Result1 row · 1 column
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
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;
$$;
Result1 row · 1 column
status
Function NORETURN_T145 successfully created.
Call
CALL NORETURN_T145();
Result1 row · 1 column
NORETURN_T145
NULL

A null. But the work still happened, and we can check.

Query
SELECT COUNT(*) AS ROWS_IN_IT FROM SALES_T145 WHERE SALE_ID = 98;
Result1 row · 1 column
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
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;
$$;
Result1 row · 1 column
status
Function OUTER_T145 successfully created.
Call
CALL OUTER_T145(10);
Result1 row · 1 column
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
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;
$$;
Result1 row · 1 column
status
Function BIGSALES_T145 successfully created.

Created happily. Now let's call it.

Call
CALL BIGSALES_T145(80.00);
Query failed — Snowflake returned an error
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
CREATE OR REPLACE TABLE SALES_T145B (
    SALE_ID INT,
    REGION  VARCHAR(16),
    AMOUNT  NUMBER(10,2)
);
Result1 row · 1 column
status
Table SALES_T145B successfully created.
Insert
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);
Result1 row · 1 column
number of rows inserted
4

And here is the procedure with the dead DEFAULT line removed.

Create
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;
$$;
Result1 row · 1 column
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
CALL BIGSALES_T145B(80.00);
And the statement straight after it
SELECT SALE_ID, THE_AMOUNT
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
ORDER BY SALE_ID;
Result3 rows · 2 columns
SALE_IDTHE_AMOUNT
1100.00
2250.50
4310.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.

Query
SELECT COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'PUBLIC' AND TABLE_NAME = 'SALES_T145B'
ORDER BY ORDINAL_POSITION;
Result3 rows · 2 columns
COLUMN_NAMEDATA_TYPE
SALE_IDNUMBER
REGIONTEXT
AMOUNTNUMBER

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
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;
$$;
Result1 row · 1 column
status
Function WITHDEFAULT_T145B successfully created.
Call
CALL WITHDEFAULT_T145B();
Result4 rows · 2 columns
SALE_IDTHE_AMOUNT
1100.00
2250.50
375.25
4310.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.

Query
SELECT COUNT(*) AS ROWS_BEFORE FROM SALES_T145;
Result1 row · 1 column
ROWS_BEFORE
5

Here is a procedure that begins a transaction, inserts a row, and commits.

Create
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;
$$;
Result1 row · 1 column
status
Function COMMITS_T145 successfully created.
Call
CALL COMMITS_T145();
Result1 row · 1 column
COMMITS_T145
committed

Straightforward. Now the same shape with ROLLBACK instead of COMMIT.

Create
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;
$$;
Result1 row · 1 column
status
Function ROLLSBACK_T145 successfully created.
Call
CALL ROLLSBACK_T145();
Query failed — Snowflake returned an error
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.

Query
SELECT SALE_ID, REGION FROM SALES_T145 WHERE SALE_ID IN (50, 51) ORDER BY SALE_ID;
Result1 row · 2 columns
SALE_IDREGION
50kept

One row, the committed one. And nothing was left open.

Query
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION;
Result1 row · 1 column
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
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;
$$;
Result1 row · 1 column
status
Function ROLLBACK1_T145B successfully created.
Call
CALL ROLLBACK1_T145B();
Result1 row · 1 column
ROLLBACK1_T145B
rolled back

No error at all. The same three statements in the same order.

I cannot explain this one

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
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;
$$;
Query failed — Snowflake returned an error
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
CALL ROLLBACK2_T145B();
Query failed — Snowflake returned an error
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
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;
$$;
Result1 row · 1 column
status
Function ROLLBACK3_T145B successfully created.
Call
CALL ROLLBACK3_T145B();
Result1 row · 1 column
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.

Query
SELECT SALE_ID, REGION FROM SALES_T145B
WHERE SALE_ID IN (50, 51, 52, 53)
ORDER BY SALE_ID;
Result1 row · 2 columns
SALE_IDREGION
50committed

One row, and the region on it says which procedure wrote it: the one that committed. Everything the rolling back procedures inserted is gone.

Query
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION;
Result1 row · 1 column
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
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;
$$;
Result1 row · 1 column
status
Function LEAVESITOPEN_T145 successfully created.
Call
CALL LEAVESITOPEN_T145();
Query failed — Snowflake returned an error
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?

Query
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION;
Result1 row · 1 column
THE_OPEN_TRANSACTION
NULL
Query
SELECT COUNT(*) AS ROWS_IN_IT FROM SALES_T145 WHERE SALE_ID = 52;
Result1 row · 1 column
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.

Statement
ROLLBACK;
Result1 row · 1 column
status
Statement executed successfully.
Query
SELECT CURRENT_TRANSACTION() AS THE_OPEN_TRANSACTION,
       (SELECT COUNT(*) FROM SALES_T145 WHERE SALE_ID = 52) AS THE_ROW;
Result1 row · 2 columns
THE_OPEN_TRANSACTIONTHE_ROW
NULL0

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
CREATE OR REPLACE PROCEDURE ASCALLER_T145()
RETURNS VARCHAR
LANGUAGE SQL
EXECUTE AS CALLER
AS
$$
BEGIN
    RETURN 'running as ' || CURRENT_ROLE() || ', user ' || CURRENT_USER();
END;
$$;
Result1 row · 1 column
status
Function ASCALLER_T145 successfully created.
Create
CREATE OR REPLACE PROCEDURE ASOWNER_T145()
RETURNS VARCHAR
LANGUAGE SQL
EXECUTE AS OWNER
AS
$$
BEGIN
    RETURN 'running as ' || CURRENT_ROLE() || ', user ' || CURRENT_USER();
END;
$$;
Result1 row · 1 column
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.

Query
SHOW PROCEDURES LIKE 'AS%\\_T145';
Result2 rows · 16 columns · scroll →
created_onnameschema_nameis_builtinis_aggregateis_ansimin_num_argumentsmax_num_argumentsargumentsdescriptioncatalog_nameis_table_functionvalid_for_clusteringis_securesecretsexternal_access_integrations
2026-10-03 13:55:07.316000-07:00ASCALLER_T145PUBLICNNN00ASCALLER_T145() RETURN VARCHARuser-defined procedureDEMO_DBNNN
2026-10-03 13:55:07.468000-07:00ASOWNER_T145PUBLICNNN00ASOWNER_T145() RETURN VARCHARuser-defined procedureDEMO_DBNNN

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
CREATE OR REPLACE PROCEDURE SAYHELLO_T145()
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
    RETURN 'hello from a procedure';
END;
$$;
Result1 row · 1 column
status
Function SAYHELLO_T145 successfully created.
Call
CALL SAYHELLO_T145();
Result1 row · 1 column
SAYHELLO_T145
hello from a procedure

Nothing about rights anywhere in it. Now let's ask Snowflake to write that procedure out for us.

Query
SELECT GET_DDL('PROCEDURE', 'SAYHELLO_T145()') AS THE_DEFAULT_DDL;
Result1 row · 1 column
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
CREATE OR REPLACE PROCEDURE LABEL_T145(N FLOAT)
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
    RETURN 'a number: ' || :N;
END;
$$;
Result1 row · 1 column
status
Function LABEL_T145 successfully created.
Create
CREATE OR REPLACE PROCEDURE LABEL_T145(S VARCHAR)
RETURNS VARCHAR
LANGUAGE SQL
AS
$$
BEGIN
    RETURN 'a string: ' || :S;
END;
$$;
Result1 row · 1 column
status
Function LABEL_T145 successfully created.
Call
CALL LABEL_T145(7);
Result1 row · 1 column
LABEL_T145
a number: 7
Call
CALL LABEL_T145('xyz');
Result1 row · 1 column
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.

Query
SHOW PROCEDURES LIKE 'LABEL\\_T145';
Result2 rows · 16 columns · scroll →
created_onnameschema_nameis_builtinis_aggregateis_ansimin_num_argumentsmax_num_argumentsargumentsdescriptioncatalog_nameis_table_functionvalid_for_clusteringis_securesecretsexternal_access_integrations
2026-10-03 13:55:08.291000-07:00LABEL_T145PUBLICNNN11LABEL_T145(FLOAT) RETURN VARCHARuser-defined procedureDEMO_DBNNN
2026-10-03 13:55:08.425000-07:00LABEL_T145PUBLICNNN11LABEL_T145(VARCHAR) RETURN VARCHARuser-defined procedureDEMO_DBNNN

Which means a DESCRIBE that does not say which overload it means cannot be answered.

Query
DESCRIBE PROCEDURE LABEL_T145();
Query failed — Snowflake returned an error
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
CREATE OR REPLACE TASK CALLER_T145
    WAREHOUSE = COMPUTE_WH
    SCHEDULE  = '1 MINUTE'
AS
    CALL ADDROW_T145(60, 60.00);
Result1 row · 1 column
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.

Statement
EXECUTE TASK CALLER_T145;
Result1 row · 1 column
status
Task CALLER_T145 is scheduled to run immediately.
Query
SELECT COUNT(*) AS ROWS_IN_IT FROM SALES_T145 WHERE SALE_ID = 60;
Result1 row · 1 column
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.

Statement
ALTER TASK IF EXISTS CALLER_T145 SUSPEND;
Result1 row · 1 column
status
Statement executed successfully.

One last thing: what a procedure refuses

A few quick ones to finish. First, no RETURNS clause at all.

Create
CREATE OR REPLACE PROCEDURE NORETURNS_T145()
LANGUAGE SQL
AS
$$
BEGIN
    RETURN 1;
END;
$$;
Query failed — Snowflake returned an error
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
CREATE OR REPLACE PROCEDURE NOBLOCK_T145()
RETURNS INT
LANGUAGE SQL
AS
$$
    RETURN 1;
$$;
Query failed — Snowflake returned an error
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
CALL DOUBLE_T145(1, 2);
Query failed — Snowflake returned an error
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
CALL NO_SUCH_PROCEDURE_T145(1);
Query failed — Snowflake returned an error
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
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;
$$;
Result1 row · 1 column
status
Function NOTABLE_T145 successfully created.
Call
CALL NOTABLE_T145();
Query failed — Snowflake returned an error
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 BEGIN and END, 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.
  • CALL cannot go in a SELECT list (002139) or a FROM clause (002142), and two of them cannot share one statement.
  • The argument, though, is ordinary SQL. A session variable or an expression is fine.
  • An INSERT a function refused works in a procedure, and the caller sees the change at once.
  • DDL works in a procedure body too.
  • A procedure's RETURNS clause is enforced when the RETURN runs. A function's is not.
  • A declared scale is applied, which is the quiet version of the same thing.
  • No RETURN statement gives a null, and the work inside the body still happens.
  • RETURNS TABLE with a RESULTSET hands rows back, and the next statement can read them with RESULT_SCAN.
  • A leftover ? in a DEFAULT clause is 092240, even when the line is dead code.
  • COMMIT inside a procedure works. A plain ROLLBACK errored in one run and worked in another, and neither run kept the row or left a transaction open.
  • ROLLBACK TRANSACTION spelled out is a syntax error inside a block. Rolling back from an EXCEPTION handler 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 OWNER is 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 DESCRIBE has to say which one it means.
  • EXECUTE TASK schedules a run. The next statement will very likely look before anything has happened.
Next in this series

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.