Platform SDK: Transaction Server |
SQL Server’s Transact-SQL programming language has been extended to include the Begin Distributed Transaction statement. A SQL Server explicitly initiated transaction works as follows:
A resource manager that initiates and participates in MS DTC transactions must reside on a system on which the Complete MS DTC Service has been installed. For information on installation and configuration of MS DTC, refer to "Setting Up an MS DTC System” in the MS DTC Administrator’s Guide and Programmer’s Reference.
The following example illustrates how a distributed MS DTC transaction can be used within a stored procedure to ensure that two SQL Server databases are updated consistently. The stored procedure explicitly initiates the distributed transaction using the Transact-SQL BEGIN DISTRIBUTED TRANSACTION statement.
/*******************************************************/ /* Using BEGIN DISTRIBUTED TRANSACTION for explicit */ /* server initiated transactions. */ /*******************************************************/ CREATE PROCEDURE change_addr(@au_id varchar(11), @addr varchar(40), @toserver varchar(12) ) AS declare @execstr varchar(200) -- 1. Start a Transaction BEGIN DISTRIBUTED TRANSACTION -- 2. Change local author information update authors set address = @addr where au_id = @au_id -- 3. Make a string with the server name, procedure to -- execute and parameters select @execstr = @toserver '.pubs..update_addr ' -- 4. Update remote server -- ( Note that these servers must be added to each -- other via sp_addserver and sp_addremotelogin ) exec @execstr @au_id, @addr -- 5. Commit the MS DTC transaction COMMIT TRANSACTION /*******************************************************/ /* Stored procedure to update an author's address on */ /* the remote server. */ /*******************************************************/ CREATE PROCEDURE update_addr(@au_id varchar(11), @addr varchar(40)) AS update authors set address = @addr where au_id = @au_id