Selecting from a stored procedure¶
Some stored procedures return tabular data. To select and manipulate this tabular data, you can call these stored procedures in the FROM clause of a SELECT statement.
Run a SELECT statement with the TABLE keyword¶
When calling the stored procedure, omit the CALL command. Instead, use the TABLE keyword, and name the procedure inside parentheses:
Example that selects from a stored procedure¶
This example uses the data in the following table:
The following stored procedure returns order information based on a user ID:
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
The following SELECT statement retrieves the stored procedure’s results:
Limitations for selecting from a stored procedure¶
The following limitations apply to selecting from a stored procedure:
Only stored procedures that perform SELECT, SHOW, DESCRIBE, or CALL statements can be placed in the FROM clause of a SELECT statement. Stored procedures that make modifications using DDL or DML operations aren’t allowed. For stored procedures that issue CALL statements, these limitations apply to the stored procedures that are called.
Only stored procedures that return tabular data with a static output schema can be placed in the FROM clause of a SELECT statement. The output columns must be named and typed. For example, a stored procedure with the following RETURNS clause is supported:
A stored procedure with the following RETURNS clause is not supported because it doesn’t return tabular data:
A stored procedure with the following RETURNS clause is not supported because it doesn’t provide a fixed output schema:
The stored procedure must be called in the FROM clause of a SELECT block in one of the following statements:
The stored procedure can’t accept correlated input arguments from their outer scope, such as a reference to any CTE defined outside of the SELECT statement.
If an argument contains a subquery, then that subquery can’t use a CTE defined by the WITH clause.
A SELECT statement containing a stored procedure call can’t be used in the body of a view, a user-defined function (UDF), a user-defined table function (UDTF), or in objects such as row access policies and data masking policies.
You can’t use bind variables in a SELECT statement that calls a stored procedure. For example, the following SELECT statements aren’t allowed: