SQL Server revisit transaction _mssql

Source: Internet
Author: User
Tags one table rollback savepoint create database
Why Use Transactions
When more than one table is updated, an execution of the article fails. In order to maintain the integrity of the data, transaction rollback is required.
Show settings transaction
Copy Code code as follows:

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

Implicit setting of transactions
Copy Code code as follows:

Set implicit_transactions on; --Start an implicit transaction
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; --Rolling back transactions
End Catch
Set Implicit_transactions off; --Turn off implicit transactions
Go

Show transactions The following statement cannot be used, and an implicit transaction can
Copy Code code as follows:

ALTER DATABASE;
Backup
Create database;
drop database;
Reconfigure;
Restore
Update STATISTICS;

Show transactions can be nested using
Copy Code code as follows:

--Create a stored procedure
CREATE PROCEDURE Qiantaoproc
@asd NCHAR (10)
As
Begin
Begin try
BEGIN TRANSACTION Innertrans
Save Transaction SavePoint--Create a transaction save point
Insert into Shiwu (ASD) values (@asd);
Commit TRANSACTION Innertrans
End Try
Begin Catch
ROLLBACK TRANSACTION SavePoint--roll back to save point
Commit TRANSACTION Innertrans
End Catch
End
Go
BEGIN TRANSACTION Outrans
exec qiantaoproc ' ASDASD ';
ROLLBACK TRANSACTION Outrans

Transaction nesting, when the outer transaction is rolled back, there is an exception if the transaction within the nesting has been rolled back. You need to use the transaction savepoint at this point. As on the code.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.