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.