EXECUTE IMMEDIATE

statementsupportedUDFs, procedures, tasks, alerts, scripting

docs.snowflake.com ↗

Executes 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

NameRequiredDescription
'string_literal' or variable or session_variablerequiredA 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 ... ] )optionalSpecifies 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