T-SQL (transaction) of the database family

Source: Internet
Author: User

What is a transaction?


A transaction is an indivisible operation that either executes all or does not execute

What's the use of business?


Ensure the complete execution of a business.

How do you use business?


3.1 Classification of transactions
Show transactions: Explicitly specify the start of a transaction with BEGIN TRANSACTION, which is the most common type of transaction.
Implicit transaction: Sets the implicit transaction mode to open by setting the SET IMPLICIT_TRANSACTIONS on statement, and the next statement automatically starts a new transaction. When the transaction completes, the next T-SQL statement starts a new transaction.
Autocommit transactions: This is the default mode for SQL Server, which treats each individual T-SQL statement as a transaction and automatically commits if executed successfully, or automatically if an error occurs.

3.2 T-SQL uses the following statements to manage transactions
Start transaction: Begin TRANSACTION
Commit TRANSACTION: Commit TRANSACTION
ROLLBACK TRANSACTION: ROLLBACK TRANSACTION
Once the transaction commits or rolls back, the transaction ends.

3.3 Explanation of three operation statements for management transactions
To determine if the T-SQL statement is wrong, use the global variable @ @errorthat we've spoken about,
It can only determine if the current bar T-SQL statement is executing with errors (the error returns a value other than 0), and the transaction contains a lot of steps, so we generally need to accumulate a bug that indicates whether there is an error in the whole transaction.

/*-- Key statement explanation--*/BEGIN TRANSACTION /*-- Define variables that are used to accumulate errors during transaction execution--*/DECLARE @errorSum INT SET @errorSum=0  --Initialize to 0, i.e. no error/*--Transfer: Zhang San account is less than 1000 yuan, John Doe account more than 1000 yuan*/UPDATEBankSETCurrentmoney=Currentmoney- + WHERECustomerName='Zhang San'SET @errorSum=@errorSum+@ @errorUPDATEBankSETCurrentmoney=Currentmoney+ + WHERECustomerName='John Doe'SET @errorSum=@errorSum+@ @error  --Cumulative ErrorIF @errorSum<>0  --If there is an error  BEGIN    Print 'transaction failed, ROLLBACK TRANSACTION'    ROLLBACK TRANSACTION   END  ELSE  BEGIN    Print 'transaction success, COMMIT transaction, write to hard disk, permanent save'    COMMIT TRANSACTION     ENDGOPrint 'view balance After a transfer transaction'SELECT *  fromBankGO
Declare @errorcount int begin Tran  Set @errorcount = 0 Insert  intoDbo. T_customer (Id,name,age)Values(1,1,'1' ) Set @errorcount =  @errorcount + @ @error UpdateT_userSetName= NULL whereId= 2 Set @errorcount =  @errorcount + @ @error if @errorcount!=0      begin          Print Convert(nvarchar( -),@errorcount)+ 'rolling back'         rollback      End  Else      begin         Print Convert(nvarchar( -),@errorcount)+ 'Submit'        Commit      End  Go

T-SQL (transaction) of the database family

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.