托了幾天的難題,終於解決了。特分享一下
以下是一個存儲過程
CREATE PROCEDURE deleteAccountAll
@id int
AS
SET XACT_abort ON
BEGIN DISTRIBUTED TRAN
delete from AccountCapital where accountid = @id
delete from logininfo where username = @id
delete from CapitalRecord where accountid = @id
delete from [Trade]..[order] where accountid = @id
delete from account where id = @id
if @@error<>0
rollback tran
else
commit tran
GO
其中order表與其他幾個表不在同一個數據庫實例,若想實現事務操作,具體要注意兩點:
1.打開MSDTC服務,即 set XACT_abort on ,若本機沒有啟動,則運行 net start msdtc
2.對不同數據庫實例的訪問要注意用[數據庫實例名]..[表名]