在触发器主体中指定触发器的父架构
在 DB2 for IBM System i 中,我创建了此触发器,用于记录 MYLOGTABLE
上对 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;
DBMS 将触发器主体存储为 < code>MYSCHEMA.MYLOGTABLE 硬编码。
现在假设我们将整个模式复制为新模式NEWSCHEMA
。当我在 NEWSCHEMA.MYCHECKEDTABLE
中插入记录时,日志记录将被添加到 MYSCHEMA.MYLOGTABLE
而不是 NEWSCHEMA.MYLOGTABLE
,即在架构中触发器及其表所在的位置。这是造成大问题的原因!!另外,因为许多用户可以在没有我控制的情况下复制架构...
那么,有没有办法在触发器主体中指定触发器所在的架构?通过这种方式,我们将编写正确的 MYLOGTABLE
中的日志记录。像PARENT SCHEMA
之类的东西...或者有解决方法吗? 非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HLL 中定义的外部触发器可以访问触发器缓冲区,其中包括触发触发器的表的库名称。这可用于限定对 MYLOGTABLE 的引用。
请参阅 IBM 红皮书第 11.2 章“触发器程序结构” 存储过程、触发器和用户定义有关更多信息,请访问 DB2 Universal Database for iSeries 上的函数。
或者,您可以使用
CURRENT SCHEMA
特殊寄存器或GET DESCRIPTOR
语句找出触发器和/或表当前所在的位置。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 theGET DESCRIPTOR
statement to find out where the trigger and/or table are currently located.不幸的是,我意识到触发器所在的模式无法从触发器体内检测。
但有一些解决方法(感谢 @krmilligan 也):
CPYLIB
的权限并让他们使用实用程序。CPYLIB
,将TRG
选项的默认值设置为*NO
。通过这种方式,触发器将永远不会被复制,除非用户明确指定它。我选择最后一种,因为它是最简单的一种,即使可能存在需要触发器复制的上下文。在这种情况下,我会采取第一种解决方法。
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):
CPYLIB
and make them use a utility.CPYLIB
set the default forTRG
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.