テーブルBから削除する削除トリガーがテーブルAにあります。トリガーが失敗したときに何が起こるかをテストしています。そのために、トリガーが見つからないようにテーブルBの名前を変更しました。
これらは私の通常の手順です。
begin transaction
delete from C
delete from A -- this errors for reason mentioned
-- At this point the transaction is automatically rolled-back.
ただし、次の手順を実行すると、次のようになります。
begin transaction
delete from C
delete from B -- this errors for reason mentioned
-- At this point transaction is not rolled back, and I can still commit it.
最初のシナリオでは、トランザクションがロールバックしているのはなぜですか?ロールバックまたはコミットのいずれかを呼び出すのはアプリケーション次第ではありませんか?
そして、全体の違いは、同じ理由でトリガーの失敗とステートメントの失敗です。動作は同じであると思います。
編集して例を追加します。
create table A (a int primary key)
create table B (a int primary key)
create table C (a int primary key)
create trigger Atrig on A for delete as delete B from B, deleted where B.a=deleted.a
insert into A values(1)
insert into A values(2)
insert into B values(2)
insert into B values(3)
insert into C values(1)
insert into C values(2)
insert into C values(3)
次に、テーブルBの名前をB2に変更します(UIを使用して名前を変更したため、SQLコマンドを使用していません)
begin transaction
delete C where a=3
delete A where a = 2
上記はこのエラーを返し、トランザクションをロールバックします。
System.Data.OleDb.OleDbException (0x80040E37): [42000]
[Message Class: 16]
[Message State: 1]
[Transaction State: 0]
[Server Name: sybase_15_5_devrs1]
[Procedure Name: Atrig]
[Line Number: 1]
[Native Code: 208]
[ASEOLEDB]B not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output).
しかし、私がこれを行う場合:
begin transaction
delete C where a=3
delete B where a = 2
上記はエラーを返しますが、トランザクションはロールバックされず、「committransaction」を発行できます。
System.Data.OleDb.OleDbException (0x80040E37): [42000]
[Message Class: 16]
[Message State: 1]
[Transaction State: 0]
[Server Name: sybase_15_5_devrs1]
[Native Code: 208]
[ASEOLEDB]B not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output).
この動作はこのトピックと関係があると思い ます。「データ変更のエラーによって引き起こされるロールバック」の表には、次のように書かれています。
Context: Transaction only
Behavior: Current command is aborted. Previous commands are not rolled back, and subsequent commands are executed.
Context: Trigger in a transaction
Behavior: Trigger completes, but trigger effects are rolled back.
All data modifications since the start of the transaction are rolled back. If a transaction spans multiple batches, the rollback affects all of those batches.
Any remaining commands in the batch are not executed. Processing resumes at the next batch.