代码之家  ›  专栏  ›  技术社区  ›  Fredou

您认为该存储过程模板有任何问题吗?

  •  -1
  • Fredou  · 技术社区  · 16 年前

    所以如果我有 A. 使命感 B 和 B C 有个错误,我可以 正确地 回滚我的东西,并返回一个stacktrace,上面写着: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
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Rubens Farias    16 年前

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