您发现该存储过程模板有什么问题吗?

发布于 2024-08-13 07:10:32 字数 1607 浏览 5 评论 0原文

所以我创建了(我使用了在其他网站上找到的一些东西)来处理事务并在执行存储过程时进行某种堆栈跟踪,该存储过程可以调用需要事务等的其他存储过程,

所以如果我有 A 调用 BB 正在调用 CC 出现错误,我可以正确 回滚我的东西并返回一个堆栈跟踪说: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

so I created that(I used some stuff found on other website) to handle transactions and having a sort of stacktrace while executing stored procedure that could call other stored procedure that need transaction and etc.

so if I have A calling B and B is calling C and C got an error, I can correctly rollback my stuff and returning a stacktrace saying: error in C follow the trace to find out where/how/etc...

do any of you find a problem with this logic?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-08-20 07:10:32

不,我看不出这段代码有什么问题。

No, I can't spot anything wrong with this code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文