SQL Server Transactions

Source: Internet
Author: User
Tags try catch

A Program execution unit (unit) that accesses and may update various data items in a database consists of multiple SQL statements that must be executed as a whole
These SQL statements are submitted to the system as a whole, either executed or not executed

Syntax steps:
To start a transaction: Begin TRANSACTION--Open transaction
Transaction Commit: Commit TRANSACTION--commit action
Transaction rollback: ROLLBACK TRANSACTION--Cancel operation

--Build TableCREATE TABLE [ Person]([PersonId]    NVARCHAR( -)PRIMARY KEY,---PRIMARY Key[PersonName] NVARCHAR( -)NULL)
/*if only begin TransAction and commit TransAction even error, it will not rollback the Select * from person*/Begin TransAction        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('3','Name3')Commit TransAction/*if only the begin TransAction and rollback TransAction have not been presented with an error, the Select * from person will be rolled back*/--Clear DataDelete PersonBegin TransAction        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('3','Name3')RollBack TransAction/*set XACT_ABORT on, in the event of an error in the transaction, the system rolls back the transaction by default, but only for non-custom errors valid SET Xact_abort OFF, default value, in the transaction, rollback of a statement or the entire transaction depending on the severity of the error program, user-level errors Errors generally do not roll back the entire transaction Select * from person*/SETXact_abort on --OpenBegin TransAction        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('3','Name3')Commit TransActionSETXact_abortOFF --Close/*Try Catch Mate transaction using Select * from person*/BeginTryBegin TransAction        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('1','Name1')        Insert  intoPerson (personid,personname)Values('3','Name3')    Commit TransActionEndTryBeginCatchRollback TransActionEndCatch/*Use the global variable @ @Error mate transactions using Select * from person*/DECLARE @tran_error int;SET @tran_error = 0; Begin TransAction        Insert  intoPerson (personid,personname)Values('1','Name1')        SET @tran_error = @tran_error + @ @ERROR; Print(@tran_error); Insert  intoPerson (personid,personname)Values('1','Name1')        SET @tran_error = @tran_error + @ @ERROR; Print(@tran_error); Insert  intoPerson (personid,personname)Values('3','Name3')        SET @tran_error = @tran_error + @ @ERROR; Print(@tran_error);IF(@tran_error > 0)    BEGIN        --performing an error, rolling back a transaction        ROLLBACK TransAction; ENDELSE    BEGIN        --no exception, COMMIT transaction        COMMIT TransAction; END

SQL Server Transactions

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.