Why use transactions?
When updating multiple tables, an execution fails. Transaction rollback is required to maintain data integrity.
Display Set transactions
Begin Try Begin Transaction Insert Into Shiwu (ASD) Values ( ' Aasdasda ' ); Commit Transaction End Try Begin Catch Select Error_number () As Errornumber Rollback Transaction End Catch
Implicitly set transactions
Set Implicit_transactions On ; -- Start implicit transactions Go Begin Try Insert Into Shiwu (ASD) Values ( ' Aasdasda ' ); Insert Into Shiwu (ASD) Values ( ' Aasdasda ' ); Commit Transaction ; End Try Begin Catch Select Error_number () As Errornumber Rollback Transaction ; -- Roll back a transaction End Catch Set Implicit_transactions Off ; -- Disable implicit transactions Go
Show that the following statements of the transaction are not available, and the implicit transaction can
Alter database;
Backup;
Create database;
Drop database;
Reconfigure;
Restore;
Update statistics;
Show that transactions can be nested
-- Create a stored procedure Create Procedure Qiantaoproc @ ASD Nchar (10 ) As Begin Begin Try Begin Transaction Innertrans Save Transaction Savepoint -- Create a transaction retention point Insert Into Shiwu (ASD)Values ( @ ASD ); Commit Transaction Innertrans End Try Begin Catch Rollback Transaction Savepoint -- Roll back to the Save point Commit Transaction Innertrans End Catch End Go Begin Transaction Outrans Exec Qiantaoproc ' Asdasd ' ; Rollback Transaction Outrans
transaction nesting. When an outer transaction is rolled back, an exception occurs if the nested transaction has been rolled back. In this case, you need to use the transaction storage point. Code .