Subject Re: Query a table within its trigger
Author hanszorn2000
--- In firebird-support@yahoogroups.com, Thomas Steinmaurer <ts@...> wrote:
>
> > I am wondering if there are any known issues with queries within a trigger, when that query accesses the table on which the trigger fires (before insert, so no race condition).
> > I am experiencing unpredictable and even wrong results in such a case, at least when using FB 2.0 and 2.5. With 2.1 I have not experienced this, so far.
> > Thanks for any help.
>
> Can you post an example on what you are trying to accomplish?
>
>
No problem! This is a simplified version of a trigger that fires Before Insert on table ARTICLE:

declare LOCATION integer;
declare ARTICLEID varchar(20);
BEGIN
select first 1 NEWLOCATION, ARTICLEID from ARTICLE
where ARTICLE.CUSTID = NEW.CUSTID
and ARTICLE.ISACTIVE = 'F'
and ARTICLE.NEWLOCATION is not null
order by NEWLOCATION
into :LOCATION, :ARTICLEID;

NEW.NEWLOCATION = LOCATION;
end

This then (sometimes) assigns values of NEWLOCATION for Articles with ISACTIVE = 'T'.

Thanks,
Hans