5

以下のコードから、raiseerror で例外が発生しています - 現在のトランザクションはコミットできず、ログ ファイルに書き込む操作をサポートできません。トランザクションをロールバックします。

IF @insertOrUdate = 'D'
BEGIN

    -- DescType depends on CorrectionType and is also a protected sync table, 
    --  it needs to be cleared out before we can remove this type
    IF EXISTS(
        SELECT TOP 1 * 
        FROM [dbo].[DescType]
        WHERE 
            [CorrectionTypeId] = @correctionTypeId

    )
    BEGIN
    PRINT 'raise error'
        RAISERROR('Dependent Desc Role Type Rollups must be removed prior to removing a type that they depend on', 16, 1)

        PRINT 'after raise error'
    END

    -- Delete protected Sync record
    DELETE FROM [dbo].[CorrectionType] WHERE [CorrectionTypeId] = @correctionTypeId;

END;
4

2 に答える 2

6

SET XACT_ABORT ON' when you do yourRAISERROR() XACT_STATE` が -1 になっているためyou're setting the、データベースに対してこれ以上コミット可能な作業を行うことができないため、トランザクションをロールバックすることしかできません。

temp procs と上記のトリガーの 1 つを使用した例:

create proc #a 
as
--This is the proxy for the parent proc
begin try
begin tran
    exec #b
commit tran
end try
begin catch
    if @@trancount > 0 rollback
    select error_message();
end catch

go

create proc #b
as
set xact_abort on; 

begin try;
    DISABLE TRIGGER [dbo].[trg_dml_CorrectionType_InsteadOfDelete] ON [dbo].[CorrectionType];
    --Check  state
    select xact_state() one;

    raiserror('Error!', 16,1)

    --This one doesn't run of course
    select xact_state() two
end try
begin catch
    select xact_state() three;
    select error_message() as msgprior;

ENABLE TRIGGER [dbo].[trg_dml_CorrectionType_InsteadOfDelete] ON [dbo].[CorrectionType];

    --This doesn't run either, new error
    select xact_state() four;

    --if @@trancount > 0 rollback transaction;
    declare @error nvarchar(2500)
    select @error = error_message()
    raiserror(@error, 16,1);
end catch

GO

exec #a

いくつかのオプションがあります。

  1. このプロシージャの XACT_ABORT を OFF に設定します。これは、この特定のシナリオの XACT_STATE 問題を処理する必要があり、ROLLBACK はトリガーの問題を処理する必要があります。
  2. ENABLE/DISABLE トリガーを親 proc に移動し、それらを完全にトランザクションの外で処理します。この子プロシージャの他のアクションに依存する必要はありません。とにかく、常にそれらを無効/有効にします。
于 2013-04-17T05:43:02.190 に答える
0

私の場合、これは環境問題であることが判明しました。分散トランザクション コーディネーター サービスが停止していました。この Windows サービスを再起動すると、問題が解決しました。

于 2014-09-03T11:15:33.927 に答える