2012-05-01 81 views
1

テーブル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) 

今B2に、テーブルBの名前を変更する(私はそれの名前を変更する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 

が、「トランザクションがロールバックされていない、と私はトランザクションをissue'commitすることができます:私はこれを行う場合、私は行動を考えている

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). 

はとは何かを持っています表「データ変更でエラーによるロールバック」ではthis topic 、それは言う:

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. 
+0

トリガーでテーブルの名前を変更しましたか?最初のトリガーはトリガーにエラーを出し、2番目のトリガーは削除自体にエラーを与えます。あなたはロールバックトランザクションコマンドを持っていますか? –

+0

aF、私は例を追加しました。トリガーは明示的にロールバックを呼び出さず、単に削除を発行します。 – Alex

答えて

1

コンテキスト:トランザクションのみ
動作:現在のコマンドは中断されます。以前のコマンドはロールバックされず、その後のコマンドが実行されます。

コンテキスト:トランザクション内のトリガー
行動:トリガーは完了しますが、トリガーの影響はロールバックされます。 トランザクションの開始以降のすべてのデータ変更がロールバックされます。トランザクションが複数のバッチにまたがる場合、ロールバックはすべてのバッチに影響します。 バッチ内の残りのコマンドは実行されません。処理は次のバッチで再開されます。


今朝の回答で指定したのと同じマニュアルを読んだ後、私は自分のsybase環境でテストしました。

関連する問題