【问题标题】:The current transaction cannot be committed and cannot support operations that write to the log file. Roll back the transaction?当前事务无法提交,也无法支持写入日志文件的操作。回滚事务?
【发布时间】:2013-07-29 11:44:43
【问题描述】:

大家好,当我在存储过程代码上更改简单且很少的东西并且它运行良好并且如果我清除更新的代码它再次运行正常所以我不知道是什么原因时突然出现以下错误 这是错误“当前事务无法提交,并且无法支持写入日志文件的操作。回滚事务

提前致谢

ALTER Procedure [dbo].[xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]
    @English Bit = 0 , 
    @ID BigInt  =  NULL Output  , 
    @Company_ID SmallInt  =  NULL , 
    @Cust_ID NVarChar (20) =  NULL , 
    @Cust_Cat NVarChar (10) =  NULL , 
    @Debit_Limit Decimal (18,3) =  NULL , 
    @From_Date NVarChar (19) =  NULL , 
    @To_Date NVarChar (19) =  NULL , 
    @User_ID NVarChar(50) = NULL 
As
BEGIN
  Create Table #Errors (ErrorNumber int Not Null, ErrorValue nvarchar(300) Collate Arabic_CI_AS Null);
  Begin Tran trn_INV_CustDebLim_setupInsert;
  Begin Try
    Insert Into INV_Cust_Debit_Limit_setup
    (  Company_ID ,  Cust_ID ,  Cust_Cat ,  Debit_Limit ,  From_Date ,  To_Date  ) 
    Values
    ( @Company_ID , @Cust_ID , @Cust_Cat , @Debit_Limit , @From_Date , @To_Date  ) 
    set @ID = @@IDENTITY
    -- From Here Is New Part --
    declare @str nvarchar(50)
    IF(@Cust_ID IS NOT NULL)
        set @str = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx ' + @Cust_ID 
    IF(@Cust_Cat IS NOT NULL)
        set @str += 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx ' + @Cust_Cat
    set @str += ' xxxxx' +@Debit_Limit + '  xxxx xx' +@From_Date
    IF(@English = 1)
        BEGIN
            IF(@Cust_ID IS NOT NULL)
                set @str = 'Credit Limit Added On Customer ' + @Cust_ID
            IF(@Cust_Cat IS NOT NULL)
                set @str += 'Credit Limit Added On Customer Category ' + @Cust_Cat
            set @str = ' With Value ' +@Debit_Limit + ' From Date ' +@From_Date
        END
    exec usp_GLApplicationAudit @Company_ID , NULL , 10 , @User_ID , @str ,@ID ,10, NULL , NULL ,NULL ,NULL,NULL,'SAl'
    -- To Here Is New Part --
  End Try
  Begin Catch
      Insert #Errors Values(1002,ERROR_MESSAGE());      
      Goto Finished;
  End Catch
-- Return Error Records
Finished:
    -- retrieve Errors and commit/Rollbak Trans.
    If (Select count(E.ErrorNumber)
            From #Errors E Left Join GVT_Errors G
                On E.ErrorNumber = G.Err_Number
            Where G.Err_Type=0 ) > 0
        Begin
            -- The Following are Errors
            Select E.ErrorNumber As [Err_No], G.MessageA + ': ' + E.ErrorValue As [Err_Desc], Err_Source, dbo.fn_GetItemDescription('Err_Type',Cast(Err_Type As nvarchar), null, null, null, null, 0) As Err_Type_Desc, Err_Type, SeverityLevel As [Severity], CategoryID
              From #Errors E Left Join GVT_Errors G 
                On E.ErrorNumber = G.Err_Number;
            Rollback Tran trn_INV_CustDebLim_setupInsert;
        End
    Else
        Begin
            -- The Following Not Errors They are Warnings or Information
            Select E.ErrorNumber As [Err_No], G.MessageA + ': ' + E.ErrorValue As [Err_Desc], Err_Source, dbo.fn_GetItemDescription('Err_Type',Cast(Err_Type As nvarchar), null, null, null, null, 0) As Err_Type_Desc, Err_Type, SeverityLevel As [Severity], CategoryID
              From #Errors E Left Join GVT_Errors G 
                On E.ErrorNumber = G.Err_Number;
            Commit Tran trn_INV_CustDebLim_setupInsert;
        End
    DROP Table #Errors;

END

【问题讨论】:

    标签: sql stored-procedures sql-server-2008-r2


    【解决方案1】:

    在 CATCH 代码中,您必须检查XACT_STATE() 的状态并采取相应措施。有关正确处理事务和 try/catch 块的过程模板,请参阅Exception Handling and Nested Transactions

    create procedure [usp_my_procedure_name]
    as
    begin
        set nocount on;
        declare @trancount int;
        set @trancount = @@trancount;
        begin try
            if @trancount = 0
                begin transaction
            else
                save transaction usp_my_procedure_name;
    
            -- Do the actual work here
    
    lbexit:
            if @trancount = 0   
                commit;
        end try
        begin catch
            declare @error int, @message varchar(4000), @xstate int;
            select @error = ERROR_NUMBER(), @message = ERROR_MESSAGE(), @xstate = XACT_STATE();
            if @xstate = -1
                rollback;
            if @xstate = 1 and @trancount = 0
                rollback
            if @xstate = 1 and @trancount > 0
                rollback transaction usp_my_procedure_name;
    
            raiserror ('usp_my_procedure_name: %d: %s', 16, 1, @error, @message) ;
        end catch   
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多