RETURN (Snowflake Scripting) | Snowflake Documentation (original) (raw)
Returns the value of a specified expression.
For more information about returning values, see Returning a value.
Syntax¶
Where:
_expression_
An expression that evaluates to the value to return.
Usage notes¶
- A RETURN statement can be executed in:
- A stored procedure.
- An anonymous block.
- A RETURN statement returns one of the following types:
- A SQL data type
- A table. Use
TABLE(...)
in theRETURN
statement.
If your block is in a stored procedure, you must also specify theRETURNS TABLE...
clause in theCREATE PROCEDURE statement.
Note
Currently, in theRETURNS TABLE(...)
clause, you can’t specify GEOGRAPHY as a column type. This applies whether you are creating a stored or anonymous procedure.
CREATE OR REPLACE PROCEDURE test_return_geography_table_1()
RETURNS TABLE(g GEOGRAPHY)
...
WITH test_return_geography_table_1() AS PROCEDURE
RETURNS TABLE(g GEOGRAPHY)
...
CALL test_return_geography_table_1();
If you attempt to specify GEOGRAPHY as a column type, calling the stored procedure results in the error:
Stored procedure execution error: data type of returned table does not match expected returned table type
To work around this issue, you can omit the column arguments and types inRETURNS TABLE()
.
CREATE OR REPLACE PROCEDURE test_return_geography_table_1()
RETURNS TABLE()
...
WITH test_return_geography_table_1() AS PROCEDURE
RETURNS TABLE()
...
CALL test_return_geography_table_1();
If you want to return the data that a RESULTSET points to, pass the RESULTSET to TABLE(…), as shown in the example below:
CREATE PROCEDURE ...
RETURNS TABLE(...)
...
RETURN TABLE(my_result_set);
...
See Returning a RESULTSET as a table.
- You can set a variable to the return value of a stored procedure. For more information, seeUsing the value returned from a stored procedure call.
Examples¶
This example declares a variable named my_var
for use in a Snowflake Scripting anonymous block and then returns the value of the variable:
DECLARE my_var VARCHAR; BEGIN my_var := 'Snowflake'; RETURN my_var; END;
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or theexecute_stream
or execute_string
method in Python Connectorcode, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, the Classic Console, and Python Connector):
EXECUTE IMMEDIATE DECLAREmyvarVARCHAR;BEGINmyvar:=′Snowflake′;RETURNmyvar;END;DECLARE my_var VARCHAR; BEGIN my_var := 'Snowflake'; RETURN my_var; END;DECLAREmyvarVARCHAR;BEGINmyvar:=′Snowflake′;RETURNmyvar;END;;