Inline DCM

With inline DCM, you plan or deploy DCM definitions that you write inline in a SQL statement, without a DCM Project object. You write DEFINE, GRANT, and ATTACH statements in the BEGIN ... END block of an EXECUTE DCM statement. Snowflake compiles the statements, sorts them by their dependencies, and returns the same changeset as a DCM project plan.

Inline DCM supports PLAN and DEPLOY only. Because it doesn’t require a DCM Project object, you don’t need additional privileges to create one, you don’t create any files such as a manifest file, and you don’t manage a project or its artifacts. Each execution is completely ephemeral.

When to use inline DCM

Use inline DCM to:

  • Try DCM definitions without creating a DCM project.
  • Check how a change affects objects that already exist before you apply it. A plan reports every object that the definitions would change, and it fails when a change breaks an object in the block that depends on it. For a walkthrough, see Change existing objects.
  • Set up the objects that a DCM project needs before you create it, such as the project owner role, the database and schema for the project object, a warehouse, and the grants. For an example, see Set up the objects for a DCM project.
  • Change started tasks and alerts without suspending them first. For an example, see Change started tasks and alerts.
  • Apply declarative definitions from a SQL script, a Snowflake Scripting block, or a SQL stored procedure.
  • Let an AI agent propose a change. In the prompt, ask the agent to write the change as DCM definitions and to run EXECUTE DCM ... PLAN, so that it validates the change against the account and shows you the changeset before anything is deployed.

Use a DCM project instead when you want the objects managed over time. A project gives you drift detection, drops objects when you remove their definitions, keeps a deployment history, and supports Jinja templating and project assets. For details, see How inline DCM differs from a DCM project.

How inline DCM differs from a DCM project

Inline DCM has the same compilation, dependency ordering, and changeset as a DCM project. DEPLOY also runs an internal plan, the same way EXECUTE DCM PROJECT ... DEPLOY does, and then applies the changes. The differences are:

  • No managed objects: The statements and the objects they define aren’t managed by any DCM project. Every run is completely independent of any previous run. Removing a statement from a later run doesn’t drop the object that an earlier run deployed. Inline DCM doesn’t take ownership of the objects, grants, or attachments, so it doesn’t record a project mapping. It can change an object that a DCM project already manages, including with GRANT OWNERSHIP. The project keeps its mapping, and the project’s next PLAN or DEPLOY reports the drift. Inline DCM rejects a DEFINE TASK GRAPH whose root task is outside the inline body, and it doesn’t change that task.
  • No project owner role: Inline DCM always runs with caller’s rights, using the privileges of the role that runs the statement.
  • No deployment history or artifacts: Executions don’t appear in any DCM deployment history, and inline DCM doesn’t store any artifacts. Each PLAN and DEPLOY is a query, so the statement and its changeset appear in query history.
  • No templating: Definitions don’t support Jinja templating or variables.
  • No project files or assets: There is no project directory and no manifest.yml, so there are no project assets. Definitions that require a project asset, such as DEFINE CODE BUNDLE or DEFINE STREAMLIT, can’t be used.

Supported definitions

Inline DCM supports the object types, grants, and attachments listed in Supported entities in DCM Projects, except definitions that require a project asset, such as DEFINE CODE BUNDLE or DEFINE STREAMLIT.

Note

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

An inline body can define a secret. The secret value is masked in query history, the same way as for a standalone CREATE SECRET statement, and the deployed secret keeps the value.

Definitions must use fully qualified names. An unqualified name fails with an Unqualified name error.

Plan and deploy definitions

Plan the definitions first, review the changeset, and then deploy the same definitions.

Plan the definitions

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;

PLAN reports which objects would change, and it doesn’t apply any changes. It also validates the definitions against the account. For more information, see Validate definitions against the account.

Review the changeset

When you run EXECUTE DCM ... PLAN or EXECUTE DCM ... DEPLOY in a SQL file in Workspaces, the results pane opens the DCM tab, which renders the changeset in a format that’s easier for humans to read. The DCM tab lists every affected object with its operation, such as CREATE or ALTER, and you can expand an object to see its detailed changes, such as added grants and privileges:

EXECUTE DCM PLAN statement with an inline DCM block in a Workspaces SQL file, and the DCM tab of the results pane listing three CREATE and two ALTER operations, with the PUBLIC role expanded to show an added USAGE grant on schema MY_TEST_DB.TEST

The changeset also includes the objects that the definitions change but don’t define. For example, the plan of the database and the schema doesn’t define the PUBLIC role, but it reports an ALTER of PUBLIC for the new grant. The plan also reports an ALTER of the role that runs the statement, which receives the OWNERSHIP privilege on each new object, and a CREATE of the PUBLIC schema that Snowflake creates in the new database. Review these operations too, not only the objects in the block.

The result of a plan is one OBJECT value, so you can also query it. Run the following query right after EXECUTE DCM ... PLAN to list one row per operation:

SELECT
    c.value:type::VARCHAR AS operation,
    c.value:object_id:domain::VARCHAR AS object_type,
    c.value:object_id:fqn::VARCHAR AS object_name
  FROM TABLE(RESULT_SCAN(LAST_QUERY_ID())) AS r,
    LATERAL FLATTEN(INPUT => r.$1:changeset) AS c;

For the format of the changeset, see PLAN and DEPLOY output.

Validate definitions against the account

A plan compiles each definition against the current state of the account. Besides the syntax, it checks that the objects and columns that a definition references exist and that the current role can access them, that a change to an existing object, such as a new data type for a column, is one that Snowflake can apply, and that the current role has the privileges to make the change.

For example, suppose the following table exists:

CREATE TABLE MY_TEST_DB.TEST.PAYMENTS (
  ID NUMBER,
  AMOUNT NUMBER(10,2),
  METHOD VARCHAR
);

The following plan succeeds, because the table and its columns exist, and reports a CREATE of the view:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE VIEW MY_TEST_DB.TEST.PAYMENT_TOTALS AS
      SELECT METHOD, SUM(AMOUNT) AS TOTAL
        FROM MY_TEST_DB.TEST.PAYMENTS
        GROUP BY METHOD;
  END;

If the view selects a column that the table doesn’t have, the plan fails. The following plan fails with an invalid identifier 'CURRENCY' error, and nothing changes in the account:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE VIEW MY_TEST_DB.TEST.PAYMENT_TOTALS AS
      SELECT CURRENCY, SUM(AMOUNT) AS TOTAL
        FROM MY_TEST_DB.TEST.PAYMENTS
        GROUP BY CURRENCY;
  END;

If the view selects from a table that doesn’t exist, or that the current role can’t access, the plan fails with an error that names the table.

Deploy the definitions

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;

DEPLOY plans the definitions again and then applies the changes. It doesn’t execute a PLAN result that you saved earlier.

Warning

DEPLOY isn’t atomic and doesn’t run in a transaction. If its internal plan fails, no changes are applied. If it fails while it applies the changes, the changes applied before the failure remain, and Snowflake doesn’t roll them back. For example, DEPLOY fails part way when another session drops a table that a view in the block selects from, after the internal plan has succeeded. Fix the cause and run the same DEPLOY again, which applies only the remaining changes.

Change existing objects

To change an object that already exists, start from its current definition, check that the plan is empty, and then change the definitions. A plan validates the definitions in the block against the account, but it 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.

Suppose a table and a view that selects one of its columns already exist:

CREATE TABLE MY_TEST_DB.TEST.ORDERS (
  ID NUMBER,
  AMOUNT NUMBER(10,2),
  LEGACY_FLAG BOOLEAN
);

CREATE VIEW MY_TEST_DB.TEST.ORDER_SUMMARY AS
  SELECT ID, AMOUNT, LEGACY_FLAG FROM MY_TEST_DB.TEST.ORDERS;

Get their current definitions as DEFINE statements. Call GET_DDL with TRUE as the third argument, which returns fully qualified names, and replace create or replace with DEFINE:

SELECT REGEXP_REPLACE(GET_DDL('TABLE', 'MY_TEST_DB.TEST.ORDERS', TRUE), '^create or replace ', 'DEFINE ', 1, 1, 'i');
SELECT REGEXP_REPLACE(GET_DDL('VIEW', 'MY_TEST_DB.TEST.ORDER_SUMMARY', TRUE), '^create or replace ', 'DEFINE ', 1, 1, 'i');

Plan the definitions before you change anything:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE TABLE MY_TEST_DB.TEST.ORDERS (
      ID NUMBER(38,0),
      AMOUNT NUMBER(10,2),
      LEGACY_FLAG BOOLEAN
    );

    DEFINE VIEW MY_TEST_DB.TEST.ORDER_SUMMARY (
      ID,
      AMOUNT,
      LEGACY_FLAG
    ) AS
      SELECT ID, AMOUNT, LEGACY_FLAG FROM MY_TEST_DB.TEST.ORDERS;
  END;

The changeset is empty, so the definitions match the objects. If the view was created by a client that sends the statement with its trailing semicolon, such as the Snowflake CLI snow sql command, the plan reports the view’s select_query as changed. The only difference is that semicolon.

Now remove LEGACY_FLAG from the table and add STATUS. Keep the view in the block, so that the plan checks it against the new table:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE TABLE MY_TEST_DB.TEST.ORDERS (
      ID NUMBER(38,0),
      AMOUNT NUMBER(10,2),
      STATUS VARCHAR
    );

    DEFINE VIEW MY_TEST_DB.TEST.ORDER_SUMMARY (
      ID,
      AMOUNT,
      LEGACY_FLAG
    ) AS
      SELECT ID, AMOUNT, LEGACY_FLAG FROM MY_TEST_DB.TEST.ORDERS;
  END;

The plan fails with an invalid identifier 'LEGACY_FLAG' error for the view, and nothing changes in the account. If you plan the table without the view, the plan succeeds and doesn’t report the broken view.

Update the view to select STATUS, and plan again:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE TABLE MY_TEST_DB.TEST.ORDERS (
      ID NUMBER(38,0),
      AMOUNT NUMBER(10,2),
      STATUS VARCHAR
    );

    DEFINE VIEW MY_TEST_DB.TEST.ORDER_SUMMARY (
      ID,
      AMOUNT,
      STATUS
    ) AS
      SELECT ID, AMOUNT, STATUS FROM MY_TEST_DB.TEST.ORDERS;
  END;

The changeset contains an ALTER of the table, which removes LEGACY_FLAG and adds STATUS, and an ALTER of the view. Deploying these definitions drops the LEGACY_FLAG column and its data.

Examples

Set up the objects for a DCM project

A DCM project needs objects that exist before you create it: a database and a schema for the project object, an owner role with the CREATE DCM PROJECT privilege on that schema, and usually a warehouse. Plan these objects inline, and then deploy them:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE ROLE MY_DCM_OWNER;
    GRANT ROLE MY_DCM_OWNER TO ROLE SYSADMIN;

    DEFINE DATABASE MY_DCM_DB;
    DEFINE SCHEMA MY_DCM_DB.PROJECTS;

    DEFINE WAREHOUSE MY_DCM_WH
      WITH
        WAREHOUSE_SIZE = XSMALL
        AUTO_SUSPEND = 60;

    GRANT USAGE ON DATABASE MY_DCM_DB TO ROLE MY_DCM_OWNER;
    GRANT USAGE ON SCHEMA MY_DCM_DB.PROJECTS TO ROLE MY_DCM_OWNER;
    GRANT CREATE DCM PROJECT ON SCHEMA MY_DCM_DB.PROJECTS TO ROLE MY_DCM_OWNER;
    GRANT USAGE ON WAREHOUSE MY_DCM_WH TO ROLE MY_DCM_OWNER;
  END;

Also grant the owner role the privileges that the project’s definitions need, for example the CREATE DATABASE privilege on the account if the project defines databases. After you deploy these definitions, use the owner role to create the DCM project in MY_DCM_DB.PROJECTS. For more information, see Create a DCM project.

Define a procedure and a task

Plan a database, a schema, a warehouse, a SQL stored procedure, and a task that calls the procedure on that warehouse. The procedure body is one $$ string, and the task body is its own BEGIN ... END block:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE DATABASE MY_TEST_DB;
    DEFINE SCHEMA MY_TEST_DB.TEST;

    DEFINE WAREHOUSE MY_TEST_WH
      WITH
        WAREHOUSE_SIZE = XSMALL
        AUTO_SUSPEND = 300;

    DEFINE PROCEDURE MY_TEST_DB.TEST.LOAD_EVENTS()
      RETURNS VARCHAR
      LANGUAGE SQL
      AS
      $$
      BEGIN
        RETURN 'ok';
      END;
      $$;

    DEFINE TASK MY_TEST_DB.TEST.RUN_LOAD
      WAREHOUSE = MY_TEST_WH
      SCHEDULE = '60 MINUTE'
      AS
      BEGIN
        CALL MY_TEST_DB.TEST.LOAD_EVENTS();
      END;
  END;

Change started tasks and alerts

Snowflake doesn’t let you change a child task while its root task is started, or change an alert while it’s started. ALTER TASK on the child task fails with an Unable to update graph with root task ... since that root task is not suspended error, and ALTER ALERT fails with an Unable to alter property for alert ... since it is not suspended error. Inline DCM applies these changes without you suspending anything first.

Deploy a root task, a child task that runs after it, and an alert. Each definition sets the desired state STARTED, so the tasks and the alert run after the deployment:

EXECUTE DCM DEPLOY FROM
  BEGIN
    DEFINE DATABASE MY_TEST_DB;
    DEFINE SCHEMA MY_TEST_DB.TEST;
    DEFINE WAREHOUSE MY_TEST_WH
      WITH
        WAREHOUSE_SIZE = XSMALL
        AUTO_SUSPEND = 60;

    DEFINE TABLE MY_TEST_DB.TEST.EVENTS (
      ID NUMBER,
      LOADED_AT TIMESTAMP_NTZ
    );
    DEFINE TABLE MY_TEST_DB.TEST.EVENT_ISSUES (
      RAISED_AT TIMESTAMP_NTZ,
      ISSUE VARCHAR
    );

    DEFINE TASK MY_TEST_DB.TEST.LOAD_EVENTS
      WAREHOUSE = MY_TEST_WH
      SCHEDULE = '60 MINUTE'
      STARTED
      AS
        INSERT INTO MY_TEST_DB.TEST.EVENTS SELECT 1, CURRENT_TIMESTAMP();

    DEFINE TASK MY_TEST_DB.TEST.CLEAN_EVENTS
      WAREHOUSE = MY_TEST_WH
      AFTER MY_TEST_DB.TEST.LOAD_EVENTS
      STARTED
      AS
        DELETE FROM MY_TEST_DB.TEST.EVENTS WHERE ID IS NULL;

    DEFINE ALERT MY_TEST_DB.TEST.MISSING_IDS
      WAREHOUSE = MY_TEST_WH
      SCHEDULE = '60 MINUTE'
      STARTED
      IF (EXISTS (SELECT 1 FROM MY_TEST_DB.TEST.EVENTS WHERE ID IS NULL))
      THEN
        INSERT INTO MY_TEST_DB.TEST.EVENT_ISSUES SELECT CURRENT_TIMESTAMP(), 'missing ID';
  END;

Later, change the child task and the condition of the alert while they’re started. The block contains only the definitions that change:

EXECUTE DCM DEPLOY FROM
  BEGIN
    DEFINE TASK MY_TEST_DB.TEST.CLEAN_EVENTS
      WAREHOUSE = MY_TEST_WH
      AFTER MY_TEST_DB.TEST.LOAD_EVENTS
      STARTED
      AS
        DELETE FROM MY_TEST_DB.TEST.EVENTS WHERE ID IS NULL OR LOADED_AT IS NULL;

    DEFINE ALERT MY_TEST_DB.TEST.MISSING_IDS
      WAREHOUSE = MY_TEST_WH
      SCHEDULE = '60 MINUTE'
      STARTED
      IF (EXISTS (SELECT 1 FROM MY_TEST_DB.TEST.EVENTS WHERE ID IS NULL OR LOADED_AT IS NULL))
      THEN
        INSERT INTO MY_TEST_DB.TEST.EVENT_ISSUES SELECT CURRENT_TIMESTAMP(), 'missing ID';
  END;

Inline DCM finds the root task of CLEAN_EVENTS, suspends it, applies the change, and resumes it, so the root task doesn’t need to be in the block. The alert gets the new condition. After the deployment, both tasks and the alert are still started.

Change a pipeline of tables, views, and dynamic tables

A plan checks each definition against the definitions in the block that it depends on, so one plan validates a whole pipeline. Deploy a table, a view that filters it, and a dynamic table that aggregates the view:

EXECUTE DCM DEPLOY FROM
  BEGIN
    DEFINE DATABASE MY_TEST_DB;
    DEFINE SCHEMA MY_TEST_DB.TEST;
    DEFINE WAREHOUSE MY_TEST_WH
      WITH
        WAREHOUSE_SIZE = XSMALL
        AUTO_SUSPEND = 60;

    DEFINE TABLE MY_TEST_DB.TEST.RAW_ORDERS (
      ID NUMBER,
      ORDER_DATE DATE,
      AMOUNT NUMBER(10,2),
      STATUS VARCHAR
    );

    DEFINE VIEW MY_TEST_DB.TEST.ORDERS_CLEAN AS
      SELECT ID, ORDER_DATE, AMOUNT
        FROM MY_TEST_DB.TEST.RAW_ORDERS
        WHERE STATUS <> 'CANCELLED';

    DEFINE DYNAMIC TABLE MY_TEST_DB.TEST.DAILY_REVENUE
      TARGET_LAG = '1 hour'
      WAREHOUSE = MY_TEST_WH
      AS
        SELECT ORDER_DATE, SUM(AMOUNT) AS REVENUE
          FROM MY_TEST_DB.TEST.ORDERS_CLEAN
          GROUP BY ORDER_DATE;
  END;

Now rename the AMOUNT column of the view to NET_AMOUNT, and plan the table, the view, and the dynamic table together:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE TABLE MY_TEST_DB.TEST.RAW_ORDERS (
      ID NUMBER,
      ORDER_DATE DATE,
      AMOUNT NUMBER(10,2),
      STATUS VARCHAR
    );

    DEFINE VIEW MY_TEST_DB.TEST.ORDERS_CLEAN AS
      SELECT ID, ORDER_DATE, AMOUNT AS NET_AMOUNT
        FROM MY_TEST_DB.TEST.RAW_ORDERS
        WHERE STATUS <> 'CANCELLED';

    DEFINE DYNAMIC TABLE MY_TEST_DB.TEST.DAILY_REVENUE
      TARGET_LAG = '1 hour'
      WAREHOUSE = MY_TEST_WH
      AS
        SELECT ORDER_DATE, SUM(AMOUNT) AS REVENUE
          FROM MY_TEST_DB.TEST.ORDERS_CLEAN
          GROUP BY ORDER_DATE;
  END;

The plan fails with an invalid identifier 'AMOUNT' error for the dynamic table, which still selects the old column name, and nothing changes in the account. If you plan the view without the dynamic table, the plan succeeds and doesn’t report the broken dynamic table. For more information, see Change existing objects.

Update the dynamic table to select NET_AMOUNT, and plan again:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE TABLE MY_TEST_DB.TEST.RAW_ORDERS (
      ID NUMBER,
      ORDER_DATE DATE,
      AMOUNT NUMBER(10,2),
      STATUS VARCHAR
    );

    DEFINE VIEW MY_TEST_DB.TEST.ORDERS_CLEAN AS
      SELECT ID, ORDER_DATE, AMOUNT AS NET_AMOUNT
        FROM MY_TEST_DB.TEST.RAW_ORDERS
        WHERE STATUS <> 'CANCELLED';

    DEFINE DYNAMIC TABLE MY_TEST_DB.TEST.DAILY_REVENUE
      TARGET_LAG = '1 hour'
      WAREHOUSE = MY_TEST_WH
      AS
        SELECT ORDER_DATE, SUM(NET_AMOUNT) AS REVENUE
          FROM MY_TEST_DB.TEST.ORDERS_CLEAN
          GROUP BY ORDER_DATE;
  END;

The changeset contains an ALTER of the view and an ALTER of the dynamic table. Deploy the same definitions to apply both changes.

Attach tags and masking policies

Tags and masking policies on a column are attached with ATTACH statements, not set in DEFINE TABLE. Plan a table, a tag on one of its columns, and a masking policy on the same column:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE DATABASE MY_TEST_DB;
    DEFINE SCHEMA MY_TEST_DB.TEST;
    DEFINE TABLE MY_TEST_DB.TEST.CUSTOMERS (
      ID NUMBER,
      EMAIL VARCHAR
    );

    DEFINE TAG MY_TEST_DB.TEST.SENSITIVITY;
    ATTACH TAG MY_TEST_DB.TEST.SENSITIVITY = 'PII'
      TO TABLE MY_TEST_DB.TEST.CUSTOMERS COLUMN EMAIL;

    DEFINE MASKING POLICY MY_TEST_DB.TEST.EMAIL_MASK
      AS (VAL VARCHAR) RETURNS VARCHAR ->
        CASE WHEN CURRENT_ROLE() = 'MY_TEST_ANALYST' THEN VAL ELSE '***' END;
    ATTACH MASKING POLICY MY_TEST_DB.TEST.EMAIL_MASK
      TO TABLE MY_TEST_DB.TEST.CUSTOMERS COLUMN EMAIL;
  END;

Define a role and its grants

Plan a role, the privileges it needs to read a table, and the grant of that role to SYSADMIN:

EXECUTE DCM PLAN FROM
  BEGIN
    DEFINE DATABASE MY_TEST_DB;
    DEFINE SCHEMA MY_TEST_DB.TEST;
    DEFINE TABLE MY_TEST_DB.TEST.ORDERS (
      ID NUMBER,
      AMOUNT NUMBER(10,2)
    );

    DEFINE ROLE MY_TEST_ANALYST;
    GRANT USAGE ON DATABASE MY_TEST_DB TO ROLE MY_TEST_ANALYST;
    GRANT USAGE ON SCHEMA MY_TEST_DB.TEST TO ROLE MY_TEST_ANALYST;
    GRANT SELECT ON TABLE MY_TEST_DB.TEST.ORDERS TO ROLE MY_TEST_ANALYST;
    GRANT ROLE MY_TEST_ANALYST TO ROLE SYSADMIN;
  END;

Besides the CREATE of each defined object, the changeset contains an ALTER of SYSADMIN, which receives the MY_TEST_ANALYST role, although SYSADMIN isn’t defined in the block. It also contains an ALTER of the role that runs the statement, which receives the OWNERSHIP privilege on each object that the plan creates. Review the changes to these roles before you deploy, the same way as the changes to the objects in the block.