Flow operators¶
Flow operators chain SQL statements together, where the results of one statement serve as the input to another statement.
Currently, the pipe operator (->>) is the only flow operator supported by Snowflake.
Pipe¶
Pipe operators are similar to Unix pipes (|) on the command line, but for SQL statements instead of Unix
commands. To use the pipe operator, specify a series of SQL statements separated by the operator. You can specify any
valid SQL statement, such as SHOW, SELECT, CREATE, INSERT, and so on. After the first SQL statement, each
subsequent statement can take the results of any previous statement as input. In the FROM clause, a previous SQL
statement is referenced by a parameter with the dollar sign ($) and the pipe number, which is the relative
position of the statement in the chain counting back from the current statement.
The pipe operator chains the following series of SQL statements together, and the comments show the relative reference numbers for each statement:
For example, this series of SQL statements has a pipe number reference in three SELECT statements, and each one takes the results of the first SELECT statement as input:
As shown, you end the chain of SQL statements by placing a semicolon after the last statement. Don’t place a semicolon after the previous statements in the chain. The output of the entire chain is the final result of the last SQL statement. Client tools, such as SnowSQL, treat the chain of statements as a single statement.
The pipe operator provides the following benefits:
Simplifies the execution of dependent SQL statements.
Improves the readability and flexibility of complex SQL operations.
Syntax¶
Usage notes¶
Each statement produces a result that can only be consumed by a subsequent statement in the chain.
Statements are executed in their specified order. Unlike
RESULT_SCAN(LAST_QUERY_ID()), the pipe number resolves to the correct result set in the chain, whether other queries were run concurrently outside of the chain or not.When a statement consumes the results of a previous statement, the result set consumed is equivalent to the result set returned by the RESULT_SCAN function that was passed the query ID of the previous statement.
For example, these statements limit the output of the SHOW WAREHOUSES command to specific columns:
This statement uses the pipe operator to produce the same results:
The output column names for SHOW and DESCRIBE commands are generated in lowercase. If you consume a result set from a SHOW or DESCRIBE command with the pipe operator or the RESULT_SCAN function, use double-quoted identifiers for the column names in the query to ensure that they match the column names in the output that was scanned. For example, if the name of an output column is
type, then specify"type"for the identifier.A query that uses the pipe operator isn’t guaranteed to return rows in the same order as the input result set of a previous query in the chain. You can include an ORDER BY clause with the query to specify the order.
An error raised by any SQL statement stops the execution of the chain, and that error is returned to the client.
The last statement result is returned to the client.
The statements are executed as a Snowflake Scripting anonymous block.
Limitations¶
The
$nparameter is only valid in the FROM clause of a SQL statement.Each SQL statement produces a result that can only be consumed by a subsequent statement in the pipe chain. The results can’t be consumed outside of the pipe chain, except for the results of the last statement.
Bind variables aren’t supported.
Using the pipe operator in a multi-statement execution (that is, submitting multiple statements separated by
;rather than->>in a single call) from Snowflake client drivers isn’t supported.When you use the pipe operator with Snowflake Scripting, you can’t combine declaration and assignment of a RESULTSET if you use the pipe operator in the SQL statement.
For example, the following code returns an error:
The following example succeeds because it separates the declaration and assignment of a RESULTSET:
Examples¶
The following examples use the pipe operator:
Execute queries that take input from queries on multiple tables
Return the results for inserts into a table that is later dropped
Select a list of columns for the output of a SHOW command¶
Run a SHOW TABLES command, and use the pipe operator to limit the output to the created_on, name, and
owner columns for tables created after April 15, 2025.
Execute queries that take input from queries on multiple tables¶
First, create a dept_pipe_demo table and an emp_pipe_demo table, and insert data into each one:
The following example uses the pipe operator for a chain of SQL statements that perform the following operations:
Query the
dept_pipe_demotable to return rows wherednameequalsSALES.Query the
emp_pipe_demotable for employees with a salary greater than1500in theSALESdepartment, using the results of the previous query as input by specifying$1in the WHERE condition of a FROM clause.Run a query that returns the
enameandsalvalues using the results of the previous query as input by specifying$1in the FROM clause.
Note
The purpose of this example is to show how to combine a series of queries with the pipe operator. However, the same output can be achieved with a join query, and join queries typically perform better than queries combined with the pipe operator.
Return the row counts for DML operations in a transaction¶
Create a table and insert rows one by one. Chaining all the statements lets you use the pipe operator to examine the result of each INSERT statement, which represents the total number of rows inserted.
In each of the SELECT statements in the example, the $1 in the SELECT list is a shorthand reference for
the first column, not a previous result in the pipe. The $n parameter for a pipe number is only
valid in the FROM clause.
The following example uses the pipe operator for a chain of SQL statements that perform the following operations:
Begin a transaction.
Insert a row into the previously created table.
Delete rows from the table.
Update rows in the table.
Commit the transaction.
Query the number of rows that were affected by each DML operation.
Return the results for inserts into a table that is later dropped¶
This example uses the pipe operator for a chain of SQL statements that performs the following operations:
Create a table with an IDENTITY column.
Insert rows into the table.
Query the table.
Drop the table.
Query the results of pipe number
$2(the SELECT statement).
The result set consumed in the last SELECT statement is equivalent to the result set returned by the RESULT_SCAN function for the query ID of the previous SELECT statement.