Subject Re: [IBO] Trigger don´t fires!
Author Lucas Franzen
Hugo,

hugosan@... schrieb:
>
> Lucas,
> you're right; my trigger doesn't fire because it's BEFORE INSERT
> and the insert happens on the client.
> I changed the trigger and now it's AFTER INSERT, and now it fires OK.
> Thanks for your suggestion about defaults. My purpose isn't setting
> default values (it was just a sample),
> but assigning values on another table, with master-detail relation.
>
> I'll explain more:
>
> CREATE TRIGGER MyTableTrigAI FOR MyTableMaster
> AFTER INSERT AS
> BEGIN
> UPDATE MyTableDetail D
> SET D.SomeDetailField=NEW.SomeMasterField
> WHERE D.ID=NEW.ID;
> END
>
> Well, on my app I first insert some Detail records, then the
> Master record, and then I commit the transaction (with both tables).
> The problem is (I think) the order in which the posts are processed
> on the server, because when the trigger fires, he doesn't still
> find any Detail record, and the UPDATE don't works for my purpose.
> Does this give you some hint?
> Really can I to alter the order of processing INSERTS?
> Very thanks,
> Hugo.

one question:
why do you want to insert a copy of a field of the master table into the
detail table? If you want to access the details you need the
master-record anyway and within this join you can easily access this
field from the mastertable.

If you have good reasons (which I won't doubt that sometimes there are
some) then go ahead and declare a before insert trigger for the detail
table rather than an after insert for the master table; within this
trigger you can select the value from the masterrecord and assign it,
like for example (there might be neater ones, I know).

CREATE TRIGGER MYDETAILAI FOR MYDETAIL AFTER INSERT
AS
DECLARE VARIBALE MASTER_DETAILFIELD VARCHAR (XX);
BEGIN
SELECT MYMASTER_DETAILFIELD FROM MASTERTABLE WHERE MASTER_ID =
:NEW.MASTER_ID
INTO :MASTER_DETAILFIELD;

NEW.MY_DETAIL_FIELD = :MASTER_DETAILFIELD;

END;

Ofcourse details can NEVER be inserted before their masters, otherwise
it would be a violation of the foreign key constraint.
Thus you can never let an insert trigger of a master table do something
on your details (ok, it could create some detail records, which in fact
can make sense); but due to the nature of a foreign key it will never be
able to update anything in the detail table)

HTH
Luc.