EXECUTE DCM

Plans or deploys DCM definitions that you write inline, without a DCM Project object. This form is called inline DCM. For when to use inline DCM, how it differs from a DCM project, and more examples, see Inline DCM.

See also:

EXECUTE DCM PROJECT

EXECUTE DCM ... PLAN performs a dry run. It validates whether the definitions would run successfully and reports their impact on existing objects in the changeset, but doesn’t apply any changes. EXECUTE DCM ... DEPLOY applies those changes.

Syntax

EXECUTE DCM { PLAN | DEPLOY } FROM
  BEGIN
    <definitions>
  END

Where <definitions> contains DEFINE, GRANT, and ATTACH statements. You can define the object types, grants, and attachments listed in Supported entities, except definitions that require a project asset, such as DEFINE CODE BUNDLE or DEFINE STREAMLIT. For details, see Supported definitions.

Note

During the preview, the list of supported definitions is subject to change.

Snowflake compiles the statements, sorts them by their dependencies, and then executes them. You can write the statements inside the BEGIN ... END block in any order.

Required parameters

PLAN

Instructs Snowflake to perform a dry run. Snowflake analyzes the changes that would be applied, but doesn’t apply any changes.

DEPLOY

Applies the changes defined in the BEGIN ... END block. DEPLOY first plans the changes, and then applies them. It doesn’t execute a PLAN result that you saved earlier.

DEPLOY isn’t atomic and doesn’t run in a transaction. If the internal plan fails, no changes are applied. If the deployment fails while it applies the changes, the changes applied before the failure remain, and Snowflake doesn’t roll them back. For more information, see Deploy the definitions.

FROM BEGIN ... END

The inline definitions. FROM is required.

Access control requirements

A role used to execute this operation must have the following privileges at a minimum:

The statement runs with the privileges of the primary role that executes it, and ignores secondary roles. The role must own each existing object that the definitions change or grant privileges on, and it needs the privilege to create each new object.

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.

Output

EXECUTE DCM ... PLAN and EXECUTE DCM ... DEPLOY return one row that contains a JSON changeset. For the format, see PLAN and DEPLOY output. The metadata object contains the timestamp, query_id, user, role_name, and command fields, where command is PLAN or DEPLOY.

In Workspaces, the results pane renders the changeset on the DCM tab. For more information, see Review the changeset.

Query history shows each EXECUTE DCM statement as one query. The individual changes that DEPLOY applies, such as creating a table or granting a privilege, don’t appear as separate queries. To see what a DEPLOY changed, review the changeset that it returns.

Usage notes

  • Run EXECUTE DCM ... PLAN before EXECUTE DCM ... DEPLOY. PLAN validates the definitions against the account: it reports whether the definitions have valid syntax, whether the objects and columns they reference exist, which objects would change, and whether the current role has the privileges to make those changes. DEPLOY doesn’t execute a saved PLAN result. For an example, see Validate definitions against the account.
  • A successful PLAN doesn’t guarantee that DEPLOY succeeds. Other sessions can change or drop the objects that the definitions reference while DEPLOY runs. If that happens while DEPLOY applies the changes, DEPLOY can fail after it has applied some of them, and those changes aren’t rolled back. Fix the cause and run the same DEPLOY again, which applies only the remaining changes.
  • Definitions must use fully qualified names. An unqualified name fails with an Unqualified name error.
  • Bind variables aren’t supported in the definitions. A definition that contains a bind variable fails, for example with a Bind variable ? not set error.
  • A plan doesn’t recheck objects outside the block that depend on the objects that you change. When you change an object, also include the views and other objects that depend on it, so that the plan reports a change that breaks them. For a walkthrough that starts from the current definitions of existing objects, see Change existing objects.
  • A definition body can contain its own BEGIN ... END block, for example the body of a task or a SQL stored procedure. The outer BEGIN ... END is the inline DCM script. The inner block belongs to that one definition.
  • Enclose a stored procedure body in $$ delimiters. Don’t put another $$ ... $$ string inside that body. A nested dollar-quoted string terminates the body. To include that text, use an ordinary quoted string instead.

Supported clients and interfaces

You can run EXECUTE DCM from a client that sends the whole statement to Snowflake, including the semicolons inside the BEGIN ... END block. A client that splits SQL text at semicolons sends only the text up to the first semicolon, and Snowflake rejects that text with a syntax error.

Client or interfaceSupport
WorkspacesSupported in SQL files. The results pane renders the changeset on the DCM tab.
Snowflake CLIThe snow sql command supports EXECUTE DCM in version 3.35.0 and later. Earlier versions split the block. The snow stage execute command isn’t supported, because it runs files with EXECUTE IMMEDIATE FROM.
Snowflake Scripting, stored procedures, tasks, and alertsSupported. You can run the statement in a Snowflake Scripting block, in a SQL, Python, JavaScript, or Java stored procedure, as the body of a task, or as the action of an alert.
EXECUTE IMMEDIATE and EXECUTE IMMEDIATE FROMNot supported. Call a stored procedure that runs the statement instead.
Python connectorSupported with cursor.execute() and cursor.execute_async(). Not supported with connection.execute_string() or connection.execute_stream(), which split the block.
Snowpark PythonSupported with session.sql(...).collect() and session.sql(...).show(). Don’t use to_pandas(): it returns an error after Snowflake has run the statement, so an EXECUTE DCM ... DEPLOY is applied even though to_pandas() fails.
JDBC, Go, and .NET driversSupported. To get the changeset, use a method that returns a result set: executeQuery() in JDBC, QueryContext() in Go, or ExecuteReader() in .NET. execute() and executeUpdate() in JDBC, ExecContext() in Go, and ExecuteNonQuery() in .NET run the statement but don’t return the changeset.
Node.js driverSupported.
Snowflake SQL APISupported.
Snowflake Terraform providerSupported in the snowflake_execute resource.
SnowSQLNot supported with the default ; delimiter, because SnowSQL splits the block. Use Snowflake CLI instead.

Other tools that split SQL scripts at semicolons have the same limitation, for example schemachange, or the Snowflake provider for Apache Airflow with its default settings. Turn off statement splitting in the tool if it has an option for it, or call a stored procedure that runs the statement.

To send EXECUTE DCM together with other statements in one request, use the multi-statement mode of the client, for example by setting the MULTI_STATEMENT_COUNT parameter to 0. The whole EXECUTE DCM statement counts as one statement.

Examples

Plan the creation of a database and a schema, and a grant on the schema. The schema definition is written before the database definition. Inline DCM compiles the statements, sorts them, and creates the database first:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE SCHEMA MY_TEST_DB.TEST;
    DEFINE DATABASE MY_TEST_DB;
    GRANT USAGE ON SCHEMA MY_TEST_DB.TEST TO ROLE PUBLIC;
  END;

Deploy the same definitions after you have reviewed the plan:

EXECUTE DCM DEPLOY FROM
  BEGIN
    DEFINE SCHEMA MY_TEST_DB.TEST;
    DEFINE DATABASE MY_TEST_DB;
    GRANT USAGE ON SCHEMA MY_TEST_DB.TEST TO ROLE PUBLIC;
  END;

For more examples, including how to change existing objects, attach tags and masking policies, and define roles and grants, see Inline DCM.