Transact-SQL 隱含交易
DB-Library 應用程式及 Transact-SQL 指令碼使用 Transact-SQL SET IMPLICIT_TRANSACTIONS ON 陳述式,來啟動隱含交易 (Implicit Transaction) 模式。使用 SET IMPLICIT_TRANSACTIONS OFF 陳述式來關閉隱含交易模式。使用 COMMIT TRANSACTION、COMMIT WORK、ROLLBACK TRANSACTION、或是 ROLLBACK WORK 陳述式來結束每一筆交易。
SET QUOTED_IDENTIFIER OFF;
GO
SET NOCOUNT OFF;
GO
USE AdventureWorks;
GO
CREATE TABLE ImplicitTran
(Cola int PRIMARY KEY,
Colb char(3) NOT NULL);
GO
SET IMPLICIT_TRANSACTIONS ON;
GO
-- First implicit transaction started by an INSERT statement.
INSERT INTO ImplicitTran VALUES (1, 'aaa');
GO
INSERT INTO ImplicitTran VALUES (2, 'bbb');
GO
-- Commit first transaction.
COMMIT TRANSACTION;
GO
-- Second implicit transaction started by a SELECT statement.
SELECT COUNT(*) FROM ImplicitTran;
GO
INSERT INTO ImplicitTran VALUES (3, 'ccc');
GO
SELECT * FROM ImplicitTran;
GO
-- Commit second transaction.
COMMIT TRANSACTION;
GO
SET IMPLICIT_TRANSACTIONS OFF;
GO