EXECUTE IMMEDIATE FROM

statementsupportedUDFs, procedures, tasks, alerts, scripting

docs.snowflake.com ↗

EXECUTE IMMEDIATE FROM executes the SQL statements specified in a file in a stage. The file can contain SQL statements or Snowflake Scripting blocks. The statements must be syntactically correct SQL statements. You can use the EXECUTE IMMEDIATE FROM command to execute the statements in a file from any Snowflake session. This feature provides a mechanism to control the deployment and management of your Snowflake objects and code. For example, you can execute a stored script to create a standard Snowflake environment for all your accounts. The configuration script might include statements that create users, roles, databases, and schemas for every new account.

Syntax

supportedpartialnot supportednot scored

EXECUTE IMMEDIATE
  FROM { absoluteFilePath | relativeFilePath }
  [ USING ( <key> => <value> [ , <key> => <value> [ , ... ] ]  )  ]
  [ DRY_RUN = { TRUE | FALSE } ]

Where

absoluteFilePath ::=
   @[ <namespace>. ]<stage_name>/<path>/<filename>

Where

relativeFilePath ::=
  '[ / | ./ | ../ ]<path>/<filename>'

Parameters

NameRequiredDescription
namespacerequiredDatabase and/or schema in which the internal or external stage resides, in the form of database_name.schema_name or schema_name. The namespace is optional if a database and schema are currently in use for the user session; otherwise, it is required.
stage_namerequiredName of the internal or external stage.
pathrequiredCase-sensitive path to the file in the stage.
filenamerequiredName of the file to execute. It must contain syntactically correct and valid SQL statements. Each statement must be separated by a semicolon.
pathrequiredCase-sensitive relative path to the file in the stage. Relative paths support established conventions such as a leading / to indicate the root of a stage’s file system, ./ to refer to the current directory (the directory the parent file is located in) and ../ to refer to the parent directory. For more information, see Usage notes.
filenamerequiredName of the file to execute. It must contain syntactically correct and valid SQL statements. Each statement must be separated by a semicolon.

Usage notes

Jinja2 templating

EXECUTE IMMEDIATE FROM can also execute a template file using the Jinja2 templating language. A template can contain variables and expressions, enabling the use of loops, conditionals, variable substitution, macros, and more. Templates can also include other templates and can import macros defined in other files located on a stage. For more information about the templating language, see the Jinja2 documentation. The template file to be executed must be: - A syntactically valid Jinja2 template. - Located in a stage or Git repository clone. - Able to render syntactically valid SQL statements. Templating enables more flexible control structures and parameterization using environment variables. For example, you can use a template to dynamically choose the deployment target of the objects defined in the script. To use a template to render a SQL script, use the templating directive or add a USING clause with at least one template variable. You can use either one of the two templating directives. The recommended directive uses valid SQL syntax: Optionally, you can use the alternative directive: Note Only a byte order mark and up to 10 whitespace characters (newlines, tabs, spaces) may be placed in front of the directive. Any characters that come after the directive on the same line will be ignored. A template can load other staged files either directly through the SnowflakeFile API or through Jinja2’s include, import, and inheritance features. Files can be referenced by absolute paths: Include, import, and extends also support relative paths while the SnowflakeFile API supports scoped Snowflake file URLs: See also:: EXECUTE IMMEDIATE

Access control requirements

- The role used to execute the EXECUTE IMMEDIATE FROM command must have the USAGE (external stage) or READ (internal stage) privilege on the stage where the file is located. - The role used to execute the file can only execute the statements in the file for which it has privileges. For example, if there is a CREATE TABLE statement in the file, the role must have the necessary privileges to create a table in the account or the statement fails. Operating on an object in a schema requires at least one privilege on the parent database and at least one privilege on the parent schema. For instructions on creating a custom role with a specified set of privileges, see Creating custom roles. For general information about roles and privilege grants for performing SQL actions on securable objects, see Overview of Access Control.

Templating usage notes

- Variable names in templates are case-sensitive. - The template variable name can be optionally enclosed in double quotes. Enclosing the variable name can be useful if any reserved keywords are used as variable names. - The following parameter types are supported in the USING clause: String. Must be enclosed by ' or $$. For example, USING (a => 'a', b => $$b$$). - Number (decimal and integer). For example, USING (a => 1, b => -1.23). - Boolean. For example, USING (a => TRUE, b => FALSE). - NULL. For example, USING (a => NULL). Note The Jinja2 templating engine interprets a NULL value as the Python NoneType type. - Session variables. For example, USING (a => $var). Only session variables holding values of supported data types are allowed. - Bind variables. For example, USING (a => :var). Only bind variables holding values of supported data types are allowed. You can use bind variables to pass stored procedure arguments to a template. - Files in Snowflake Git repositories or in Snowflake Native Apps cannot be accessed from the template. - The maximum result size for template rendering is 100,000 bytes. - Templates are rendered using the Jinja2 version 3.1.6 templating engine.

Troubleshooting EXECUTE IMMEDIATE FROM errors

This section contains some common errors that result from an EXECUTE IMMEDIATE FROM statement and how you can resolve them. - File errors - Stage errors - Access control errors - Templating errors Error | 001501 (02000): File '<directory_name>' not found in stage '<stage_name>'. Cause | There are multiple causes for this error: The file does not exist. The file name is the root of a directory. For example @stage_name/scripts/. Solution | Verify the name of the file and confirm the file exists. Executing all the files in a directory is not supported. Error | 001503 (42601): Relative file references like '<filename.sql>' cannot be used in top-level EXECUTE IMMEDIATE calls. Cause | The statement was executed using a relative file path outside of a file execution. Solution | A relative file path can only be used in EXECUTE IMMEDIATE FROM statements in a file. Use the absolute file path for the file. For more information, see Usage notes. Error | 001003 (42000): SQL compilation error: syntax error line <n> at position <m> unexpected '<string>'. Cause | The file contains SQL syntax errors. Solution | Fix the syntax errors in the file and reupload the file to the stage. Error | 002003 (02000): SQL compilation error: Stage '<stage_name>' does not exist or not authorized. Cause | The stage does not exist or you do not have access to the stage. Solution | Verify the name of the stage and confirm the stage exists. Execute the statement using a role that has the required privileges to access the stage. For more information, see Access control requirements. Error | 003001 (42501): Uncaught exception of type 'STATEMENT_ERROR' in file <file_name> on line <n> at position <m>: SQL access control error: Insufficient privileges to operate on schema '<schema_name>' Cause | The role used to execute the statement does not have the privileges required to execute some or all of the statements in the file. Solution | Use a role that has the appropriate privileges to execute the statements in the file. For more information, see Access control requirements. See also: Stage errors. Error | 001003 (42000): SQL compilation error: syntax error line [n] at position [m] unexpected '{'. Cause | The file contains templating constructs (for example, {{ table_name }}) but is not rendered using the templating engine. If the template is not rendered, the lines of text in the template are executed as SQL statements. The templating constructs in the file are likely to result in SQL syntax errors. Solution | Add a templating directive or re-execute the statement with the USING clause and specify at least one template variable. Error | 000005 (XX000): Python Interpreter Error: jinja2.exceptions.UndefinedError: '<key>' is undefined in template processing Cause | If any variables used in the template are left unspecified in the USING clause, an error occurs. Solution | Verify the names and number of variables in the template and update the USING clause to include values for all template variables. Error | 001510 (42601): Unable to use value of template variable '<key>' Cause | The value for the variable key is an unsupported type. Solution | Verify that you are using a supported parameter type for the template variable value. For more information, see the Templating usage notes. Error | 001518 (42601): Size of expanded template exceeds limit of 100,000 bytes. Cause | The size of the rendered template exceeds the current limit. Solution | Split your templated file into multiple smaller templates and add a new script to execute them sequentially, while passing down template variables to the nested scripts.