Use transactions in SQL SERVER Stored Procedures

Source: Internet
Author: User

Create PROCEDURE UpdateWanjun
@ UserName nvarchar (500 ),
@ UserPassword nvarchar (500 ),
@ ReturnVal int output
AS
-- Set XAcT_ABORT ON
Begin Transaction T
Update admins set UserPassword = @ UserPassword where UserName = @ UserName
Update admins set UserPassword = @ UserPassword, a = 'aaadfasdfasdfas 'where id = 4 -- error statement. Type a is int and the default size is 4.
-- Set @ ReturnVal = @ Rowcount (this variable can only be used under the SQL statement next to it, so that the returned value can be returned. It can only be used under Update)

IF @ Error <> 0
Begin
Print '20140901'
RollBack Transaction T
End
Else
Print '20140901'
COMMIT Transaction T
GO

######################################## ######################################## ####

If you call a stored procedure on another server in a stored procedure, you need to use

Begin distributed transaction to start transactions (DISTRIBUTED transactions)

Begin distributed tran [SACTION]
[Transaction_name| @Tran_name_variable]

Parameters

Transaction_name

Is a user-defined transaction name used to track distributed transactions in the ms dtc utility.Transaction_nameMust comply with the identifier rules, but only use the first 32 characters.

@Tran_name_variable

Is a user-defined variable name that contains a transaction name used to track distributed transactions in the ms dtc utility. RequiredChar,Varchar,NcharOrNvarcharThis variable is declared as a data type.

Note

The server that executes the begin distributed transaction statement is the TRANSACTION creator and controls the completion of the TRANSACTION. When a connection sends a subsequent commit transaction or rollback transaction statement, the master server requests the ms dtc to manage the completion of distributed transactions between the involved servers.

There are two ways to register a remote SQL server in a distributed transaction:

  • A registered connection in a distributed transaction executes a remote stored procedure call, which references a remote server.

  • A registered connection in a distributed transaction executes a distributed query that references a remote server.

For exampleServerAInitiate begin distributed transaction, this connection is calledServerBStored Procedures andServerCAndServerCStored ProcedureServerDWhen a distributed query is executed, all the four SQL servers enter the distributed transaction.ServerAIs the creator and control server of the transaction.

The connection involved in the Distributed Transaction Transact-SQL does not obtain the transaction object that can be passed to another connection, and thus it cannot be explicitly registered in the distributed transaction. The only way for a remote server to register a transaction is to become the target of remote stored procedure calling or distributed query.

By default, any valid user has the transin distributed transaction permission.

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.