触发器中的错误似乎回滚整个事务

时间:2012-05-01 21:41:19

标签: sybase-ase

我在表A上有一个从表B删除的删除触发器。我正在测试触发器失败时会发生什么,为此我重命名了表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.

为什么在第一个场景中,交易正在回滚?不应该由应用程序调用rollback或commit吗?

整个差异是触发失败而声明失败的原因相同,我希望行为完全相同。

编辑以添加示例:

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

Above返回此错误,并回滚事务:

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
上面的

返回错误,但是事务没有回滚,我可以发出'commit transaction':

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.

1 个答案:

答案 0 :(得分:1)

上下文:仅限交易
行为:当前命令已中止。不回滚先前的命令,并执行后续命令。

上下文:在交易中触发
行为:触发器完成,但会回滚触发器效果。 自事务开始以来的所有数据修改都将回滚。如果事务跨越多个批次,则回滚会影响所有这些批次。 批处理中的任何剩余命令都不会执行。处理将在下一批处理中恢复。


在阅读了今天早上在答案中指定的相同手册之后,我已经在我的sybase环境中进行了测试以及发生了什么。