In DB2 for IBM System i I create this trigger for recording on MYLOGTABLE every insert operation made on MYCHECKEDTABLE:
SET SCHEMA MYSCHEMA;
CREATE TRIGGER MYTRIGGER AFTER INSERT ON MYCHECKEDTABLE
REFERENCING NEW AS ROWREF
FOR EACH ROW BEGIN ATOMIC
INSERT INTO MYLOGTABLE -- after creation becomes MYSCHEMA.MYLOGTABLE
(MMACOD, OPTYPE, OPDATE)
VALUES (ROWREF.ID, 'I', CURRENT TIMESTAMP);
END;
The DBMS stores the trigger body with MYSCHEMA.MYLOGTABLE hardcoded.
Now imagine that we copy the entire schema as a new schema NEWSCHEMA. When I insert a record in NEWSCHEMA.MYCHECKEDTABLE a log record will be added to MYSCHEMA.MYLOGTABLE instead of NEWSCHEMA.MYLOGTABLE, i.e. in the schema where trigger and its table live. This is cause of big issues!! Also because many users can copy the schema without my control…
So, is there a way to specify, in the trigger body, the schema where the trigger lives? In this way we’ll write the log record in the correct MYLOGTABLE. Something like PARENT SCHEMA… Or is there a workaround?
Many thanks!
Unfortunately I realized that the schema where a trigger lives can’t be detected from inside trigger’s body.
But there are some workarounds (thanks to @krmilligan too):
CPYLIBand make them use a utility.CPYLIBset the default forTRGoption to*NO. In this way triggers will never be copied, except if the user explicitly specifies it.I choose the last one because it’s the simplest one, even if there can be contexts where trigger copy is required. In such cases I’d take the first workaround.