- Categories:
TO_QUERY¶
Returns a result set based on SQL text and an optional set of arguments that are passed to the SQL text if it is parameterized. The function compiles the SQL text as the definition of a subquery in the FROM clause. When writing an application or a stored procedure, you can call this function to construct a SQL statement.
Note
This function can include user input in query statements, which has potential security risks, such as SQL injection. If inputs to the function come from external sources, make sure they are validated. For more information, see SQL injection.
- See also:
Syntax¶
Arguments¶
Required
SQL => 'string'String representation of the subquery.
Optional
arg => 'value'Bind variables passed to the SQL
string.
Returns¶
Returns the result set produced by the execution of the specified SQL text or NULL. If any argument is NULL, the function returns NULL without reporting any error.
Usage notes¶
All arguments must be one of the following:
Constant strings.
SQL variables or Snowflake Scripting variables that resolve to strings.
If you need to convert a string passed in an argument to a different data type, you can use a conversion function in the SQL
stringto convert the string to another data type.The set of columns defining the result set is derived from the SELECT list of the compiled SQL statement.
The function is valid only in the FROM clause of a SQL statement.
Examples¶
Create a table and insert data into it.
The examples use the data in this table.
Using TO_QUERY in SQL statements¶
First, set a session variable (SQL variable) for the table name:
The examples use the session variable and IDENTIFIER() to identify the table.
Using IDENTIFIER() to identify database objects is a best practice because it can make code more reusable and help to prevent SQL injection risks.
The following example uses the TO_QUERY function to return all of the data in the to_query_example table:
The following example uses the TO_QUERY function to return all of the values in the deptno column of
the to_query_example table:
The following example uses the TO_QUERY function to pass an argument to a SQL statement so that it returns the row
where deptno equals 10 in the to_query_example table:
The following example is the same as the previous example, but it uses a session variable to pass
the deptno value to the TO_QUERY function:
The following example uses the TO_QUERY function to pass two arguments to a SQL statement so that it returns the rows
where deptno equals 10 or dname equals SALES in the to_query_example table:
Using TO_QUERY in stored procedures¶
The following example uses the TO_QUERY function in a stored procedure:
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):
Call the stored procedure:
The following example uses the TO_QUERY function in a stored procedure with a bind variable:
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):
Call the stored procedure:
Call the stored procedure using a session variable: