所以我创建了那个(我用其他网站上的东西)来处理事务并在执行存储过程时具有某种堆栈跟踪,该存储过程可以调用需要事务等的其他存储过程。
因此,如果我有 A 调用 B 和 B 调用 C 和 C 出现错误,我可以正确地回滚我的东西并返回一个堆栈跟踪,说:C在跟踪后查找错误/如何/等...
你们中有人发现这个逻辑有问题吗?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[NAME]
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON
declare @trancount int
set @trancount = @@trancount
declare @savePointName varchar(40)
set @savePointName = newid()
BEGIN TRY
if @trancount = 0
begin transaction
else
save transaction @savePointName
/*
// STUFF HERE
*/
if @trancount = 0
commit transaction
END TRY
BEGIN CATCH
declare @xstate int
set @xstate = XACT_STATE()
if @xstate = -1 and @trancount = 0
rollback transaction
if @xstate = 1 and @trancount = 0
rollback transaction
if @xstate = 1 and @trancount > 0
rollback transaction @savePointName
declare @message varchar(max)
set @message = ERROR_MESSAGE() +
' (' + ERROR_PROCEDURE() +
':' + ltrim(str(ERROR_LINE())) +
', Raised ' + ltrim(str(ERROR_NUMBER())) +
', Severity ' + ltrim(str(ERROR_SEVERITY())) +
', State ' + ltrim(str(ERROR_STATE())) + ')'
RAISERROR(@message,16,1)
END CATCH
END
最佳答案
不,我无法发现此代码有任何错误。
关于sql-server-2005 - 您认为该存储过程模板有任何问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1839939/