托了几天的难题,终于解决了。特分享一下
以下是一个存储过程
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.对不同数据库实例的访问要注意用[数据库实例名]..[表名]