176

我有一个 PL/SQL 函数(在 Oracle 10g 上运行),我在其中更新了一些行。有没有办法找出 UPDATE 影响了多少行?手动执行查询时,它告诉我有多少行受到影响,我想在 PL/SQL 中获取该数字。

4

6 回答 6

275

您使用sql%rowcount变量。

您需要在需要查找受影响的行数的语句之后直接调用它。

例如:

set serveroutput ON; 
DECLARE 
    i NUMBER; 
BEGIN 
    UPDATE employees 
    SET    status = 'fired' 
    WHERE  name LIKE '%Bloggs'; 
    i := SQL%rowcount; 
    --note that assignment has to precede COMMIT
    COMMIT; 
    dbms_output.Put_line(i); 
END; 
于 2009-05-14T07:33:00.433 回答
31

对于那些想要从普通命令获得结果的人,解决方案可能是:

begin
  DBMS_OUTPUT.PUT_LINE(TO_Char(SQL%ROWCOUNT)||' rows affected.');
end;

基本问题是 SQL%ROWCOUNT 是一个 PL/SQL 变量(或函数),不能从 SQL 命令直接访问。通过使用 noname PL/SQL 块,可以实现这一点。

...如果有人有在 SELECT 命令中使用它的解决方案,我会很感兴趣。

于 2014-09-03T07:55:20.800 回答
7

或者,SQL%ROWCOUNT 您可以在过程中使用它而无需声明变量

于 2012-08-06T14:03:38.410 回答
1

SQL%ROWCOUNT也可以在不分配的情况下使用(至少从Oracle 11g开始)。

只要在当前块内没有执行任何操作(更新、删除或插入),SQL%ROWCOUNT就设置为 null。然后它保持受最后一次 DML 操作影响的行数:

假设我们有表 CLIENT

create table client (
  val_cli integer
 ,status varchar2(10)
)
/

我们会这样测试它:

begin
  dbms_output.put_line('Value when entering the block:'||sql%rowcount);

  insert into client 
            select 1, 'void' from dual
  union all select 4, 'void' from dual
  union all select 1, 'void' from dual
  union all select 6, 'void' from dual
  union all select 10, 'void' from dual;  
  dbms_output.put_line('Number of lines affected by previous DML operation:'||sql%rowcount);

  for val in 1..10
    loop
      update client set status = 'updated' where val_cli = val;
      if sql%rowcount = 0 then
        dbms_output.put_line('no client with '||val||' val_cli.');
      elsif sql%rowcount = 1 then
        dbms_output.put_line(sql%rowcount||' client updated for '||val);
      else -- >1
        dbms_output.put_line(sql%rowcount||' clients updated for '||val);
      end if;
  end loop;  
end;

导致:

Value when entering the block:
Number of lines affected by previous DML operation:5
2 clients 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
于 2016-09-15T15:18:28.117 回答
-1

请试试这个..


create table client (
  val_cli integer
 ,status varchar2(10)
);

---------------------
begin
insert into client
select 1, 'void' from dual
union all
select 4, 'void' from dual
union all
select 1, 'void' from dual
union all
select 6, 'void' from dual
union all
select 10, 'void' from dual;
end;

---------------------
select * from client;

---------------------
declare
  counter integer := 0;
begin
  for val in 1..10
    loop
      update client set status = 'updated' where val_cli = val;
      if sql%rowcount = 0 then
        dbms_output.put_line('no client with '||val||' val_cli.');
      else
        dbms_output.put_line(sql%rowcount||' client updated for '||val);
        counter := counter + sql%rowcount;
      end if;
  end loop;
   dbms_output.put_line('Number of total lines affected update operation: '||counter);
end;

---------------------
select * from client;

--------------------------------------------------------

结果将如下所示:


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


于 2018-01-23T13:56:32.433 回答
-3

使用 Count(*) 分析函数 OVER PARTITION BY NULL 这将计算总行数

于 2015-05-03T00:50:55.873 回答