EXECUTE IMMEDIATE
statementsupportedUDFs, procedures, tasks, alerts, scriptingExecutes a string that contains a SQL statement or a Snowflake Scripting statement. You can use EXECUTE IMMEDIATE to do the following: - In a Snowflake Scripting block, execute dynamic SQL, where parts of the SQL statement aren’t known until runtime. For examples, see Executing dynamic SQL in a Snowflake Scripting block. - Set a session variable to a SQL statement, and reference the session variable to run the SQL statement. For an example, see Setting a session variable to a statement and executing it. - If you are using Snowflake CLI or Snowsight, run a Snowflake Scripting anonymous block. For an example, see Running an anonymous block in Snowflake CLI or Snowsight.
Syntax
supportedpartialnot supportednot scored
EXECUTE IMMEDIATE '<string_literal>'
[ USING ( <bind_variable> [ , <bind_variable> ... ] ) ]
EXECUTE IMMEDIATE <variable>
[ USING ( <bind_variable> [ , <bind_variable> ... ] ) ]
EXECUTE IMMEDIATE $<session_variable>
[ USING ( <bind_variable> [ , <bind_variable> ... ] ) ]
Parameters
| Name | Required | Description |
|---|---|---|
'string_literal' or variable or session_variable | required | A string literal, Snowflake Scripting variable, or session variable that contains a statement. A statement can be any of the following: A single SQL statement A stored procedure call A control-flow statement (for example, looping or branching statement) A block If you use a session variable, the length of the statement must not exceed the maximum size of a session variable (16KB). |
USING ( bind_variable [ , bind_variable ... ] ) | optional | Specifies one or more bind variables that hold values to be used in the cursor’s query definition (for example, in a WHERE clause). |
Usage notes
- The string_literal, variable, or session_variable must contain only one statement. (A block is considered one statement, even if the body of the block contains multiple statements.)
- A session_variable must be preceded by a dollar sign ($).
- A local variable must not be preceded by a dollar sign ($).