SQL INSERT 是否会失败而不在 PL/SQL 中引发异常

发布于 2024-10-06 00:37:39 字数 169 浏览 4 评论 0原文

是否存在 SQL(特别是 Oracle-PL/SQL)中的 INSERT 可能失败而不引发异常的情况?我在 INSERT 之后看到代码检查,它验证 SQL%ROWCOUNT = 1 ELSE 它引发了自己的用户定义的异常。我不明白这怎么可能发生。

Are there any cases where an INSERT in SQL (specifically Oracle-PL/SQL) can fail without an exception being thrown? I'm seeing checks in code after INSERT where it verifies that SQL%ROWCOUNT = 1 ELSE it raises its own user-defined exception. I don't see how that can ever happen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

奢华的一滴泪 2024-10-13 00:37:40

正如 @TonyAndrews 和 @GinoA 提到的,INSERT 可以通过多种方式返回一行以外的内容(触发器、INSERT INTO tablename SELECT... 语法)。

但更大的问题是您使用的是 PL/SQL。因此,SQL%ROWCOUNT 值可以用作确定程序执行流程的条件,包括发出 COMMITROLLBACK 语句。

即使只是引发用户定义的异常,调用 PL/SQL 块也可以自行处理异常。

编辑:有人应该修改问题标题以指示 PL/SQL(如问题本身所示),因为这与标题建议的 SQL 范围不同。

As @TonyAndrews and @GinoA mentioned, there are several ways an INSERT could return something other than exactly one row (triggers, INSERT INTO tablename SELECT... syntax).

But the bigger issue is that you're in PL/SQL. As such, the SQL%ROWCOUNT value can be used as a condition to determine the program execution flow including issuing COMMIT or ROLLBACK statements.

Even with just raising a user-defined exception, the calling PL/SQL block can handle the exception itself.

EDIT: Someone should modify the question title to indicate PL/SQL (as indicated in the question itself), since that's not the same thing as SQL scope the title suggests.

桃扇骨 2024-10-13 00:37:39

它不可能无一例外地失败,不是。编写代码的开发人员可能不知道这一点。

可以想象,after 语句触发器可以删除刚刚插入的行。当然,INSERT...SELECT 可能找不到要插入的行,因此会导致 SQL%ROWCOUNT = 0。

It can't fail without an exception, no. Probably the developer who wrote the code didn't know that.

An after statement trigger could conceivably delete the row just inserted. And of course an INSERT...SELECT might find no rows to insert, and so would result in SQL%ROWCOUNT = 0.

夜巴黎 2024-10-13 00:37:39

除了 @mcabral 提到的基于触发器的问题之外,您可能会成功插入但插入 1 行以外的行。例如,insert into blah(col1) select col2 from foo 样式的插入。

In addition to the trigger-based issue @mcabral mentioned, you could have an insert that is successful but inserts other than 1 row. For example, the insert into blah(col1) select col2 from foo style of insert.

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