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!

有帮助吗?

解决方案 2

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):

  • Take away the user's authority to execute CPYLIB and make them use a utility.
  • Create a background agent on the system that peridiocally runs looking for triggers that are out of synch.
  • For command CPYLIB set the default for TRG option 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.

其他提示

External triggers defined in an HLL have access to a trigger buffer that includes the library name of the table that fired the trigger. This could be used to qualify the reference to the MYLOGTABLE.

See chapter 11.2 "Trigger program structure" of the IBM Redbook Stored Procedures, Triggers, and User-Defined Functions on DB2 Universal Database for iSeries for more information.

Alternatively you may be able to use the CURRENT SCHEMA special register or the GET DESCRIPTOR statement to find out where the trigger and/or table are currently located.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top