---
title: AUTO CDC INTO (pipelines)
description: Read changes from a CDC source and apply them to a streaming table in Lakeflow pipelines SQL with the `AUTO CDC ... INTO` statement.
canonical_url: https://docs.databricks.com/aws/en/ldp/developer/ldp-sql-ref-apply-changes-into
---

# AUTO CDC INTO (pipelines)

Use the `AUTO CDC ... INTO` statement to create a flow that uses Lakeflow pipelines change data capture (CDC) functionality. This statement reads changes from a CDC source and applies them to a streaming target.

* To learn about CDC, see [Change data capture and snapshots](https://docs.databricks.com/aws/en/data-engineering/what-is-cdc.md).
* For more details about using `AUTO CDC`, see [The AUTO CDC APIs: Simplify change data capture with pipelines](https://docs.databricks.com/aws/en/ldp/cdc.md).
* For more detail about `CREATE FLOW`, see [CREATE FLOW (pipelines)](https://docs.databricks.com/aws/en/ldp/developer/ldp-sql-ref-create-flow.md).

## Syntax

```
CREATE OR REFRESH STREAMING TABLE table_name;

CREATE FLOW flow_name AS AUTO CDC [ONCE] INTO table_name
FROM source
KEYS (keys)
[IGNORE NULL UPDATES [ON {(columnList) | * EXCEPT (exceptColumnList)}]]
[APPLY AS DELETE WHEN condition]
[APPLY AS TRUNCATE WHEN condition]
SEQUENCE BY orderByColumn
[SYSTEM SEQUENCE BY systemOrderByColumn]
[COLUMNS {(columnList) | * EXCEPT (exceptColumnList)}]
[STORED AS {SCD TYPE 1 | SCD TYPE 2 | BITEMPORAL}]
[TRACK HISTORY ON {(columnList) | * EXCEPT (exceptColumnList)}]
[COLUMNS TO UPDATE columnName]
```

You define data quality constraints for the target using the same `CONSTRAINT` clause as other pipeline queries. See [Manage data quality with pipeline expectations](https://docs.databricks.com/aws/en/ldp/expectations.md).

The default behavior for `INSERT` and `UPDATE` events is to *upsert* CDC events from the source: update any rows in the target table that match the specified key(s) or insert a new row when a matching record does not exist in the target table. Handling for `DELETE` events can be specified with the `APPLY AS DELETE WHEN` condition.

> [!IMPORTANT]
>
> You must declare a target streaming table to apply the changes into. You can optionally specify the schema for your target table. For SCD type 2 tables, when specifying the schema of the target table, you must also include the `__START_AT` and `__END_AT` columns with the same data type as the `sequence_by` field.

See [The AUTO CDC APIs: Simplify change data capture with pipelines](https://docs.databricks.com/aws/en/ldp/cdc.md).

## Parameters

* **`ONCE`**

  Specifying `ONCE` means that this performs a one-time insert, or backfill, into the target table. It is not re-run if the pipeline it is in gets refreshed, except in the case of a full refresh.

  This clause is optional.

* **`flow_name`**

  The name of the flow to create.

* **`source`**

  The source for the data. The source must be a **streaming** source. Use the STREAM keyword to use streaming semantics to read from the source. If the read encounters a change or deletion to an existing record, an error is thrown. It is safest to read from static or append-only sources. To ingest data that has change commits, you can use Python and the `skipChangeCommits` option to handle errors.

  For more information on streaming data, see [Transform data with pipelines](https://docs.databricks.com/aws/en/ldp/transform.md).

* **`KEYS`**

  The column or combination of columns that uniquely identify a row in the source data. The values in these columns are used to identify which CDC events apply to specific records in the target table.

  To define a combination of columns, use a comma-separated list of columns.

  This clause is required.

* **`IGNORE NULL UPDATES`**

  Allows ingesting updates containing a subset of the target columns. When a CDC event matches an existing row and `IGNORE NULL UPDATES` is specified, columns with a `null` value retain their existing values in the target. This also applies to nested columns with a `null` value.

  For partial updates, add an `ON` clause to control which columns ignore `null` values:

  * `IGNORE NULL UPDATES ON (columnList)`: only the listed columns retain their existing values when the incoming value is `null`. All other columns apply explicit `null` values.
  * `IGNORE NULL UPDATES ON * EXCEPT (exceptColumnList)`: all columns except those listed retain their existing values when the incoming value is `null`. The listed columns apply explicit `null` values.

  This clause is optional.

  The default is to overwrite existing columns with `null` values.

* **`APPLY AS DELETE WHEN`**

  Specifies when a CDC event should be treated as a `DELETE` rather than an upsert.

  For SCD type 2 sources, to handle out-of-order data, the deleted row is temporarily retained as a tombstone in the underlying Delta table, and a view is created in the metastore that filters out these tombstones. The retention interval can be configured with the `pipelines.cdc.tombstoneGCThresholdInSeconds` [table property](https://docs.databricks.com/aws/en/ldp/properties.md#table-properties).

  This clause is optional.

* **`APPLY AS TRUNCATE WHEN`**

  Specifies when a CDC event should be treated as a full table `TRUNCATE`. Because this clause triggers a full truncate of the target table, it should be used only for specific use cases requiring this functionality.

  The `APPLY AS TRUNCATE WHEN` clause is supported only for SCD type 1. SCD type 2 does not support the truncate operation.

  This clause is optional.

* **`SEQUENCE BY`**

  The column name specifying the logical order of CDC events in the source data. The pipeline processing uses this sequencing to handle change events that arrive out of order.

  If multiple columns are needed for sequencing, use a `STRUCT` expression: it orders by the first struct field first, then by the second field if there's a tie, and so on.

  Specified columns must be sortable data types.

  This clause is required.

* **`SYSTEM SEQUENCE BY`**

  The column name specifying the system time at which each CDC event is known to the system. Used with `STORED AS BITEMPORAL` to track changes across both business time (`SEQUENCE BY`) and system time. See [Bitemporal AUTO CDC](https://docs.databricks.com/aws/en/ldp/cdc-bitemporal.md).

  Specified columns must be sortable data types.

  This clause is optional and applies only to bitemporal tables.

* **`COLUMNS`**

  Specifies a subset of columns to include in the target table. You can either:

  * Specify the complete list of columns to include: `COLUMNS (userId, name, city)`.
  * Specify a list of columns to exclude: `COLUMNS * EXCEPT (operation, sequenceNum)`

  This clause is optional.

  The default is to include all columns in the target table when the `COLUMNS` clause is not specified.

* **`STORED AS`**

  Whether to store records as SCD type 1, SCD type 2, or bitemporal.

  Set to `BITEMPORAL` to track changes across both business time and system time. Bitemporal requires `SYSTEM SEQUENCE BY`. See [Bitemporal AUTO CDC](https://docs.databricks.com/aws/en/ldp/cdc-bitemporal.md).

  This clause is optional.

  The default is SCD type 1.

* **`TRACK HISTORY ON`**

  Specifies a subset of output columns to generate history records when there are any changes to those specified columns. You can either:

  * Specify the complete list of columns to track: `TRACK HISTORY ON (userId, name, city)`.
  * Specify a list of columns to be excluded from tracking: `TRACK HISTORY ON * EXCEPT (operation, sequenceNum)`

  This clause is optional. The default is to track history for all the output columns when there are any changes, equivalent to `TRACK HISTORY ON *`.

* **`COLUMNS TO UPDATE`**

  Specifies the name of a source column that holds, for each change record, the set of columns to update as an array of column-name strings (`array<string>`). Columns not in the array keep their existing target values, while listed columns are written from the source, including explicit `null` values.

  Use this clause for partial updates when each change record updates a different set of columns and you need to apply explicit `null` values.

  You cannot use `COLUMNS TO UPDATE` together with `IGNORE NULL UPDATES`, and it is not supported for bitemporal tables.

  This clause is optional.

## Notes

For SCD Type 1 targets in triggered pipelines, one or more SQL `AUTO CDC` flows can share a target with one `AUTO CDC FROM SNAPSHOT` flow defined in Python or SQL. The following requirements apply:

* Give each `AUTO CDC` flow a unique flow name.
* Use the same number of keys in the same order for all flows. Snapshot-flow key names are compared case-insensitively with `AUTO CDC` key names. Multiple `AUTO CDC` flows must use identical key names and casing.
* Use exactly the same data type for `SEQUENCE BY` and the snapshot version.
* Do not define expectations on the `AUTO CDC FROM SNAPSHOT` flow.
* Do not specify `IGNORE NULL UPDATES` or its column-list variants.
* If you add an `AUTO CDC` flow to an existing `AUTO CDC FROM SNAPSHOT` target, the target must not contain user columns whose names conflict with reserved `AUTO CDC` system columns.
* Do not use this pattern for SCD Type 2 or bitemporal targets.

For an example, see [Add a backfill to an AUTO CDC SCD Type 1 table](https://docs.databricks.com/aws/en/ldp/flows-backfill.md#auto-cdc-scd1-backfill).

## Examples

```sql
-- Create a streaming table, then use AUTO CDC to populate it:
CREATE OR REFRESH STREAMING TABLE target;

CREATE FLOW flow
AS AUTO CDC INTO
  target
FROM stream(cdc_data.users)
  KEYS (userId)
  APPLY AS DELETE WHEN operation = "DELETE"
  SEQUENCE BY sequenceNum
  COLUMNS * EXCEPT (operation, sequenceNum)
  STORED AS SCD TYPE 2
  TRACK HISTORY ON * EXCEPT (city);
```
