SQL Server 跨数据库事务
时间:2021-07-01 10:21:17
帮助过:4人阅读
TRY
BEGIN TRAN
INSERT INTO DB1.dbo.Log
( Type, Message, Stack, Created )
VALUES ( 0, -- Type - int
N‘Message‘, -- Message - nvarchar(100)
N‘Stack‘, -- Stack - nvarchar(max)
‘2010-03-12 05:43:17‘ -- Created - datetime
)
-- RAISERROR (‘插入第一个库后失败.‘, -- Message text.
--16, -- Severity.
--1 -- State.
-- );
INSERT INTO DB2.dbo.Agent
( Name ,
Mobile ,
Password ,
LocationID ,
Address ,
Status ,
CreateTime
)
VALUES ( N‘Name‘ , -- Name - nvarchar(50)
‘Mobile‘ , -- Mobile - varchar(16)
‘Password‘ , -- Password - varchar(64)
0 , -- LocationID - int
N‘Address‘ , -- Address - nvarchar(256)
0 , -- Status - int
‘2015-05-22 05:43:48‘ -- CreateTime - datetime
)
--RAISERROR (‘插入第二个库后失败.‘, -- Message text.
-- 16, -- Severity.
-- 1 -- State.
-- );
EXEC TestDB.dbo.SP_T1 --DB3存储过程
RAISERROR (‘插入存储过程后失败.‘, -- Message text.
16, -- Severity.
1 -- State.
);
COMMIT TRAN
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0---------------判断有没有事务
BEGIN
PRINT ERROR_MESSAGE()
ROLLBACK TRAN----------回滚事务
END
END CATCH
来自为知笔记(Wiz)
SQL Server 跨数据库事务
标签: