SQL Server review transactions

Source: Internet
Author: User
Tags savepoint

Why use transactions?
When updating multiple tables, an execution fails. Transaction rollback is required to maintain data integrity.
Display Set transactions Copy codeThe Code is 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

Implicitly set transactionsCopy codeThe Code is as follows: 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 the 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 canCopy codeThe Code is as follows: alter database;
Backup;
Create database;
Drop database;
Reconfigure;
Restore;
Update statistics;

Show that transactions can be nestedCopy codeThe Code is 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 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. The above 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.