"on exception" statement not firing within stored procedure when called from trigger



Hello there,

I am experiencing a problem whereby ON EXCEPTION statements are
not firing within stored procedures when the stored procedures
are invoked from a trigger.

Is this a bug, or is this behaviour documented somewhere?

Either way, it's rather annoying - can it be changed? Are there any
workarounds? I could test for whether the exception would occur first,
but that means extra work to guard against race conditions.

I can reproduce this under IDS versions 9.40.UC7 and 10.00.FC5
(both on Linux).

Here's a (fairly) minimal test case that shows the problem:

=== BEGIN SQL ===

-- Create a table called tFoo with a primary key column called "a".
-- We explicitly name the index (not that this makes any difference).

create table tFoo (a int not null) lock mode row;
create unique index iFoo_x1 on tFoo(a);
alter table tFoo add constraint (
primary key (a)
constraint cFoo_pk
);

-- Create a stored procedure, pFoo, which inserts a row with a = 99
-- into tFoo. It uses the "on exception" technique to avoid raising
-- an error if such a row already exists.

create procedure pFoo ()

begin

on exception in (-268, -239)
-- Ignore a uniqueness or PK constraint violation.
end exception with resume;

insert into tFoo (a) values (99);

end

end procedure;

-- Call pFoo a few times to check it works:

execute procedure pFoo();
execute procedure pFoo();
execute procedure pFoo();

select * from tFoo;

-- Now create a table called tBar with an insert trigger which
-- calls the pFoo stored procedure.

create table tBar (b int not null, primary key (b)) lock mode row;
create trigger riBar insert on tBar
for each row
(
execute procedure pFoo()
);

-- Cause the trigger on tBar to fire:

insert into tBar (b) values (1);

=== END SQL ===

On running the above from dbaccess, I get:

268: Unique constraint (kelby.cfoo_pk) violated.
100: ISAM error: duplicate value for a record with unique key.

from the "insert into tBar ..." line.

And yet pFoo works fine when called directly.

Any feedback appreciated,
Regards,
Kieran Elby

.



Relevant Pages

  • Re: "on exception" statement not firing within stored procedure when called from trigger
    ... not firing within stored procedures when the stored procedures ... The last paragraph of the ON EXCEPTION page in the SQL Syntax guide ... "ON EXCEPTION has no effect within a UDR that is called by a trigger." ... "This is equivalent to executing a SET CONSTRAINTS ALL DEFERRED ...
    (comp.databases.informix)
  • Re: Trigger, alternative way to pass variable to trigger
    ... What we did was have our object that we used to call stored procedures add ... Then in the trigger or default constraints, I used a user defined function ... system function, though just calling it as master.. ... >>Pro SQL Server 2000 Database Design ...
    (microsoft.public.sqlserver.programming)
  • Re: SPs or triggers
    ... would be sending ad-hoc query statements from your application to SQL Server ... Stored procedures help you abstract your ... To import user data by registration whit SP or Trigger? ... > @ValuePrice BIGINT, ...
    (microsoft.public.sqlserver.programming)
  • Re: Error Handling Coming Out of a Trigger
    ... http://www.oledbdirect.com - The fastest way to access MS SQL Server, ... I have an after update trigger on a table. ... would have caused an exception in the calling application; ... FErrorMessage:= ...
    (borland.public.delphi.database.ado)
  • Re: [RFC] hwbkpt: Hardware breakpoints (was Kwatch)
    ... meaning that neither notifier will return NOTIFY_STOP. ... If it's a ptrace exception it also ... as a special case for a ptrace trigger. ...
    (Linux-Kernel)