将连接设置为隐式事务模式。
Transact-SQL 语法约定
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
SET IMPLICIT_TRANSACTIONS { ON | OFF }
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
如果设置为 ON,SET IMPLICIT_TRANSACTIONS 将连接设置为隐式事务模式。如果设置为 OFF,则使连接恢复为自动提交事务模式。
如果连接处于隐式事务模式,并且当前不在事务中,则执行下列任一语句都可启动事务:
ALTER TABLE | FETCH | REVOKE |
BEGIN TRANSACTION | GRANT | SELECT |
CREATE | INSERT | TRUNCATE TABLE |
DELETE | OPEN | UPDATE |
DROP |
|
|
如果连接已经在打开的事务中,则上述语句不会启动新事务。
对于因为此设置为 ON 而自动打开的事务,用户必须在该事务结束时将其显式提交或回滚。否则,当用户断开连接时,事务及其包含的所有数据更改将被回滚。事务提交后,执行上述任一语句即可启动一个新事务。
隐式事务模式将始终生效,直到连接执行 SET IMPLICIT_TRANSACTIONS OFF 语句使连接恢复为自动提交模式。在自动提交模式下,所有单个语句在成功完成时将被提交。
进行连接时,SQL Server Native Client OLE DB Provider for SQL Server 和 SQL Server Native Client ODBC 驱动程序会自动将 IMPLICIT_TRANSACTIONS 设置为 OFF。对于与 SQLClient 托管提供程序的连接以及通过 HTTP 端点接收的 SOAP 请求,SET IMPLICIT_TRANSACTIONS 默认为 OFF。
如果 SET ANSI_DEFAULTS 为 ON,则 SET IMPLICIT_TRANSACTIONS 也为 ON。
SET IMPLICIT_TRANSACTIONS 的设置是在执行或运行时设置的,而不是在分析时设置的。
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
以下示例阐释在将 IMPLICIT_TRANSACTIONS
设置为 ON
时,显式或隐式启动的事务。该示例中使用了 @@TRANCOUNT
函数来阐释打开的事务和关闭的事务。
USE AdventureWorks; GO SET NOCOUNT ON; GO SET IMPLICIT_TRANSACTIONS OFF; GO PRINT N'Tran count at start = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); GO IF OBJECT_ID(N'dbo.t1',N'U') IS NOT NULL DROP TABLE dbo.t1; GO CREATE table dbo.t1 (a int); GO INSERT INTO dbo.t1 VALUES (1); GO PRINT N'Use explicit transaction.'; BEGIN TRANSACTION; GO INSERT INTO dbo.t1 VALUES (2); GO PRINT N'Tran count in explicit transaction = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); COMMIT TRANSACTION; GO PRINT N'Tran count after explicit transaction = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); GO PRINT N'Setting IMPLICIT_TRANSACTIONS ON.'; GO SET IMPLICIT_TRANSACTIONS ON; GO PRINT N'Use implicit transactions.'; GO -- No BEGIN TRAN needed here. INSERT INTO dbo.t1 VALUES (4); GO PRINT N'Tran count in implicit transaction = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); COMMIT TRANSACTION; PRINT N'Tran count after implicit transaction = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); GO PRINT N'Nest an explicit transaction with IMPLICIT_TRANSACTIONS ON.'; GO PRINT N'Tran count before nested explicit transaction = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); BEGIN TRANSACTION; PRINT N'Tran count after nested BEGIN TRAN in implicit transaction = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); INSERT INTO dbo.t1 VALUES (5); COMMIT TRANSACTION; PRINT N'Tran count after nested explicit transaction = ' + CAST(@@TRANCOUNT AS NVARCHAR(10)); GO -- Commit outstanding tran. COMMIT TRANSACTION; GO
![](http://i.msdn.microsoft.com/Global/Images/clear.gif)
参考
ALTER TABLE (Transact-SQL)BEGIN TRANSACTION (Transact-SQL)
CREATE TABLE (Transact-SQL)
DELETE (Transact-SQL)
DROP TABLE (Transact-SQL)
FETCH (Transact-SQL)
GRANT (Transact-SQL)
INSERT (Transact-SQL)
OPEN (Transact-SQL)
REVOKE (Transact-SQL)
SELECT (Transact-SQL)
SET (Transact-SQL)
SET ANSI_DEFAULTS (Transact-SQL)
@@TRANCOUNT (Transact-SQL)
TRUNCATE TABLE (Transact-SQL)
UPDATE (Transact-SQL)