Oracle中BEFORE INSERT TRIGGER失败时会发生什么

发布于 2024-09-08 15:19:46 字数 533 浏览 1 评论 0原文

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流绪微梦 2024-09-15 15:19:46

即使触发器中有异常,您的异常“处理”也将确保插入成功。

一些想法:

  1. 您当前的代码不能导致 NO_DATA_FOUND 异常。

  2. 你真的希望你的代码默默地失败吗?

  3. 为什么您同时捕获 NO_DATA_FOUNDOTHERS 并忽略它们? 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:

  1. Your current code cannot cause a NO_DATA_FOUND-exception.

  2. Do you really want your code to fail silently?

  3. Why do you catch both NO_DATA_FOUND and OTHERS and ignore both? OTHERS will catch NO_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文