Oracle中BEFORE INSERT TRIGGER失败时会发生什么
我对oracle中的BEFORE INSERT TRIGGER有一个小疑问, 我的触发器看起来像这样:
CREATE OR REPLACE TRIGGER some_trigger BEFORE INSERT
ON some_table REFERENCING NEW AS newRow
FOR EACH ROW
DECLARE
some_var number(25, 4);
BEGIN
-- do some stuff
:newRow.some_column :=some_var;
exception
when no_data_found then
NULL;
when others then
NULL;
END;
这里我对 newRow.some_column 所做的更新是可选的,所以我的要求是即使触发器失败, newRow 也应该插入到表中,这就是我吃掉异常的原因。
我的假设是否正确,如果我吃掉异常,则 newRow 在所有情况下都会被插入到表中?
谢谢大家。
I have a small doubt regarding BEFORE INSERT TRIGGER in oracle,
my trigger looks like this:
CREATE OR REPLACE TRIGGER some_trigger BEFORE INSERT
ON some_table REFERENCING NEW AS newRow
FOR EACH ROW
DECLARE
some_var number(25, 4);
BEGIN
-- do some stuff
:newRow.some_column :=some_var;
exception
when no_data_found then
NULL;
when others then
NULL;
END;
Here the update which I am doing on newRow.some_column is an optional thing, so my requirement is that even the trigger fails, the newRow should be inserted into the table and this is why I am eating up exceptions.
Is my assumption correct that if I eat up exception, the newRow will be inserted into the table in all scenarios ?
Thanks heaps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使触发器中有异常,您的异常“处理”也将确保插入成功。
一些想法:
您当前的代码不能导致
NO_DATA_FOUND
异常。你真的希望你的代码默默地失败吗?
为什么您同时捕获
NO_DATA_FOUND
和OTHERS
并忽略它们?OTHERS
也会捕获NO_DATA_FOUND
。编辑
我只是抓住
NO_DATA_FOUND
并添加一个好评论,说明为什么你可以在你的案例中默默地忽略它。确保您的 SELECT 仅返回一行,否则还需要处理
TOO_MANY_ROWS
。忽略
OTHERS
通常被认为是不好的做法。您的代码可能会失败,而您却永远不会注意到。实际上,有一个新的编译器警告。Your exception "handling" will make sure that the insert succeeds, even if you have an exception in your trigger.
Some thoughts:
Your current code cannot cause a
NO_DATA_FOUND
-exception.Do you really want your code to fail silently?
Why do you catch both
NO_DATA_FOUND
andOTHERS
and ignore both?OTHERS
will catchNO_DATA_FOUND
too.EDIT
I'd just catch the
NO_DATA_FOUND
and add a good comment about why you can silently ignore it in your case.Make sure that your SELECT only returns a single row, otherwise
TOO_MANY_ROWS
needs to be handled too.Ignoring
OTHERS
is generally considered bad practice. Your code could fail and you'd never notice. There is a new Compiler Warning for this, actually.