PL/SQL 中受 UPDATE 影响的行数
我有一个 PL/SQL 函数(在 Oracle 10g 上运行),我在其中更新一些行。 有没有办法找出有多少行受到更新的影响? 手动执行查询时,它会告诉我有多少行受到影响,我想在 PL/SQL 中获取该数字。
I have a PL/SQL function (running on Oracle 10g) in which I update some rows. Is there a way to find out how many rows were affected by the UPDATE? When executing the query manually it tells me how many rows were affected, I want to get that number in PL/SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您使用
sql%rowcount
变量。您需要在需要查找受影响行数的语句之后直接调用它。
例如:
You use the
sql%rowcount
variable.You need to call it straight after the statement which you need to find the affected row count for.
For example:
对于那些想要从普通命令获得结果的人,解决方案可能是:
基本问题是 SQL%ROWCOUNT 是 PL/SQL 变量(或函数),不能直接从 SQL 命令访问。 通过使用 noname PL/SQL 块,可以实现这一点。
...如果有人有一个在 SELECT 命令中使用它的解决方案,我会很感兴趣。
For those who want the results from a plain command, the solution could be:
The basic problem is that SQL%ROWCOUNT is a PL/SQL variable (or function), and cannot be directly accessed from an SQL command. By using a noname PL/SQL block, this can be achieved.
... If anyone has a solution to use it in a SELECT Command, I would be interested.
或者,
SQL%ROWCOUNT
您可以在过程中使用它,无需声明变量
alternatively,
SQL%ROWCOUNT
you can use this within the procedure without any need to declare a variable
SQL%ROWCOUNT
也可以在不分配的情况下使用(至少从Oracle 11g)。只要当前块内未执行任何操作(更新、删除或插入),
SQL%ROWCOUNT
就会设置为 null。 然后它保留为受最后一个 DML 操作影响的行数:假设我们有表 CLIENT
我们会这样测试它:
结果: >
SQL%ROWCOUNT
can also be used without being assigned (at least from Oracle 11g).As long as no operation (updates, deletes or inserts) has been performed within the current block,
SQL%ROWCOUNT
is set to null. Then it stays with the number of line affected by the last DML operation:say we have table CLIENT
We would test it this way:
Resulting in:
2 个客户端更新为 1
没有客户端具有 2 个 val_cli。
没有客户端具有 3 个 val_cli。
1 个客户更新为 4
没有客户端具有 5 个 val_cli。
1 个客户更新为 6
没有客户端具有 7 val_cli。
没有客户端具有 8 val_cli。
没有 9 val_cli 的客户端。
1 个客户更新了 10
影响更新操作的总行数:5
2 client updated for 1
no client with 2 val_cli.
no client with 3 val_cli.
1 client updated for 4
no client with 5 val_cli.
1 client updated for 6
no client with 7 val_cli.
no client with 8 val_cli.
no client with 9 val_cli.
1 client updated for 10
Number of total lines affected update operation: 5
使用 Count(*) 分析函数 OVER PARTITION BY NULL
这将计算总行数
Use the Count(*) analytic function OVER PARTITION BY NULL
This will count the total # of rows