SQL Server Distributed Transaction instance

Source: Internet
Author: User

Copy codeThe Code is as follows:
-- Begin distributed transaction [transactionname]
-- Mark a TSQL Distributed Transaction managed by the Distributed Transaction Processing Coordinator MSDTC.
-- Server a is the master SERVER. When the connection sends a subsequent commit transaction or
-- Rollback transaction statement, the master server requests MSDTC to manage between involved servers
-- Completion of distributed transactions

-- SQLSERVER uses the linked server or remote server as the platform for distributed transaction processing.
-- Remote Stored Procedure Call and Distributed Query


-- When using distributed transactions for A remote stored procedure call and A Distributed Query
-- Issue begin distributed transaction, the connection calls the stored procedure on SERVER B
-- Another stored procedure on server c, and the stored procedure on server c executes
-- Distributed Query: Four SQLSERVER servers enter the distributed transaction. server a is the creator of the transaction.
-- And Control Server

-- Create a distributed transaction and delete a record in both the local and remote databases.
-- The Instance name is RemoteServer. The local and remote databases submit or roll back the transaction at the same time.
-- Note that four naming rules are used for executing distributed queries or calling stored procedures.

-- Prerequisite: the MSDTC Service on the local machine and the MSDTC Service on the remote machine must be enabled.
-- The local machine and remote machine can ping each other
-- Database ports can be telnet to each other
-- Create a linked server to a remote machine WIN7U-20130414Z
USE [GPOSDB]
GO
SELECT * FROM [SystemPara] WHERE [Name] = 'hdtport'
SELECT * FROM [WIN7U-20130414Z]. [GPOSDB]. dbo. [SystemPara] WHERE [Name] = 'hdtport'

 

USE [GPOSDB]
GO
BEGIN DISTRIBUTED TRANSACTION
-- Delete a record from the local database
Delete from [JOE]. [GPOSDB]. [DBO]. [SystemPara]
WHERE [Name] = 'hdport'

-- Delete a record from the remote database
Delete from [GPOSDB]. [dbo]. [SystemPara]
WHERE [Name] = 'hdport'

COMMIT TRAN
GO


-- I tried it because the two-way SQL server access adopts the chained access (Login server access ),
-- In this case, you only need to set the statement for accessing the database of the other Party:
-- Select * from linkedServerA. dbo. table1
-- Modify:
-- Select * from dbo. table1.
-- Mark it for future resolution.

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.