---
title: ATOMIC compound statement
description: Run a sequence of SQL statements as a single atomic transaction with the BEGIN ATOMIC ... END compound statement in Databricks SQL and Databricks Runtime 18.0 and above.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-txn-begin-atomic
---

# ATOMIC compound statement

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 18.0 and above

Implements a SQL Script block that can contain a sequence of SQL statements, control-of-flow statements, local variable declarations, and exception handlers. When marked as `ATOMIC`, the block runs as a transactional unit where all statements succeed together or fail together.

## Syntax

```sql
BEGIN ATOMIC
  statement1;
  statement2;
  ...
END;
```

## Parameters

None. The `ATOMIC` keyword modifies the [compound statement](https://docs.databricks.com/gcp/en/sql/language-manual/control-flow/compound-stmt.md) behavior.

## Description

Marks the compound statement as an atomic transaction block. All statements within the block run as a single transactional unit: either all statements succeed together, or all changes are rolled back if any statement fails. Databricks automatically commits changes when the block completes successfully, or rolls back all changes if any statement fails.

You cannot nest `BEGIN ATOMIC ... END` blocks. An atomic block can contain a non-atomic `BEGIN ... END` block; those statements run as part of the outer atomic transaction. You can `CALL` an atomic stored procedure from an atomic block. The procedure body is flattened into the outer transaction. All statements run as part of a single larger transaction. This is not a nested transaction.

This is a non-interactive transaction. You don't need to run `COMMIT` or `ROLLBACK` manually.

### Requirements

* All tables written to in a multi-statement, multi-table transaction must:
  * Be Unity Catalog managed tables (Delta or Iceberg)
  * Have [Catalog commits](https://docs.databricks.com/gcp/en/tables/features/catalog-commits.md) enabled
* Use a SQL warehouse, [serverless compute](https://docs.databricks.com/gcp/en/compute/serverless/index.md), or a cluster running Databricks Runtime 18.0 and above.
* You must have appropriate permissions on objects modified within the transaction. Privileges are checked when each statement runs.

## Examples

The following examples demonstrate common transaction patterns using `BEGIN ATOMIC ... END;`.

### Coordinate updates across multiple tables

```sql
BEGIN ATOMIC
  UPDATE accounts SET balance = balance - 100 WHERE id = 1;
  UPDATE accounts SET balance = balance + 100 WHERE id = 2;
  INSERT INTO audit_log VALUES (1, 2, 100, current_timestamp());
END;
```

### Validate data before committing

```sql
BEGIN ATOMIC
  INSERT INTO staging_customers
  SELECT * FROM external_source WHERE ingest_date = current_date();

  IF (SELECT COUNT(*) FROM staging_customers WHERE email NOT LIKE '%@%') > 0 THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid email addresses found';
  END IF;

  MERGE INTO customers AS target
  USING staging_customers AS source
  ON target.customer_id = source.customer_id
  WHEN MATCHED THEN UPDATE SET *
  WHEN NOT MATCHED THEN INSERT *;
END;
```

## <a id="additional-resources" /> Additional resources

* [BEGIN TRANSACTION](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-txn-begin.md): Start an interactive transaction with manual commit/rollback control
* [COMMIT](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-txn-commit.md): Commit an interactive transaction
* [ROLLBACK](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-txn-rollback.md): Roll back an interactive transaction
* [Compound statement](https://docs.databricks.com/gcp/en/sql/language-manual/control-flow/compound-stmt.md): `BEGIN ... END` blocks without the `ATOMIC` keyword
* [Transactions](https://docs.databricks.com/gcp/en/transactions/index.md): Overview of transaction support
* [Transaction modes](https://docs.databricks.com/gcp/en/transactions/transaction-modes.md): Detailed patterns and examples for non-interactive and interactive transactions
