The rollback method is often used when an SQL statement error occurs during transaction execution.

Source: Internet
Author: User
Tb_test is a table with two fields: Id auto-incrementing ID and unique ID of userid

1. Default transactions

Begin Tran insert into tb_test (userid) values (1) insert into tb_test (userid) values (2) insert into tb_test (userid) values (3) insert into tb_test (userid) values (3) -- error insert into tb_test (userid) values (5) Commit tran

In the preceding method, only the current error row can be rolled back, that is, the row marked with error, and other rows can continue to be executed.

Ii. Set xact_abort on plus transaction

Set xact_abort onbegin Tran insert into tb_test (userid) values (1) insert into tb_test (userid) values (2) insert into tb_test (userid) values (3) insert into tb_test (userid) values (3) -- error insert into tb_test (userid) values (5) Commit tran

In the above method, roll back all, that is, none of the above statements is inserted.

Iii. Set xact_abort on and general statements

Set xact_abort oninsert into tb_test (userid) values (1) insert into tb_test (userid) values (2) insert into tb_test (userid) values (3) insert into tb_test (userid) values (3) -- errorinsert into tb_test (userid) values (5)

In the preceding method, the execution is stopped when an error occurs, but the previous statement is not rolled back. The first three rows are successful, but the fifth row is not executed.

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.