COMMIT

适用于:勾选“是” Databricks SQL

提交当前交互式事务,将所有已修改表中的更改永久保存。 有关交互式事务的要求和使用模式,请参阅 交互式事务

Syntax

COMMIT [ TRANSACTION | WORK ]

参数

此语句没有参数。

备注

  • 如果没有活动事务,则执行 COMMIT 将导致 NO_ACTIVE_TRANSACTION 错误。
  • 如果 COMMIT 失败,则事务已中止,并且必须在开始新事务之前显式运行 ROLLBACK 以重置事务状态。 导致 COMMIT 失败的常见原因包括与其他并发事务的写入冲突。
  • 要执行此语句,您必须拥有一个活动事务,并对事务中读取或修改的所有对象具有相应的权限。

示例

以下示例演示如何提交交互式事务。

基本交易

单独运行每个语句。

BEGIN TRANSACTION;
-- Insert a new record
INSERT INTO my_table VALUES (1, 'test');
-- Update records in another table
UPDATE another_table
SET value = 'updated'
WHERE id = 5;
-- Commit all changes in both tables atomically
COMMIT TRANSACTION;

读取自己的写入

单独运行每个语句。

BEGIN TRANSACTION;
-- Insert new data
INSERT INTO customers VALUES (101, 'New Customer');
-- Update the newly inserted data
UPDATE customers SET name = 'Updated Customer Name' WHERE id = 101;
-- Query sees the updated value within the transaction
SELECT name FROM customers WHERE id = 101;
-- Returns 'Updated Customer Name'
-- Commit makes changes visible to other transactions
COMMIT;

处理提交失败

如果 COMMIT 因冲突或其他错误而失败,您必须显式回滚。 单独运行每个语句。

BEGIN TRANSACTION;
-- Make changes
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Attempt to commit
COMMIT;
-- If COMMIT fails (e.g., due to write conflict),
-- the transaction is aborted
-- You must explicitly roll back before starting a new transaction
ROLLBACK;

其他资源