sql%rowcount

sql%rowcount用于记录修改的条数,就如你在sqlplus下执行delete from之后提示已删除xx行一样, 这个参数必须要在一个修改语句和commit之间放置,否则你就得不到正确的修改行数。
例如:
SQL> declare n number;
  2  begin
  3  insert into test_a select level lv from dual connect by level<500;
  4  n:=sql%rowcount;
  5  commit;
  6  dbms_output.put_line(n);
  7  end;
  8  /
499

PL/SQL procedure successfully completed.
SQL> declare n number;
  2  begin
  3  insert into test_a select level lv from dual connect by level<500;
  4  commit;
  5  n:=sql%rowcount;
  6  dbms_output.put_line(n);
  7  end;
  8  /
如果我连续执行了3个select语句
然后调用sql%rowcount,得到的结果是最后一个select的条数。  要统计所有的,可以使用三个变量接收sql%rowcount,然后相加即可,不过貌似直接select的写法无法使用sql%rowcount。

 

 

 

以下转自

http://www.eygle.com/archives/2005/04/oracleoeouaeeae.html

测试过Delete和For all两种方法,for all在这里并没有带来性能提高,所以仍然选择了批量直接删除。

create or replace procedure delBigTab(p_TableName in varchar2,
                                      p_Condition in varchar2,
                                      p_Count     in varchar2)
aspragma autonomous_transaction;
 n_delete number := 0;
  begin
    while 1 = 1 loopEXECUTE IMMEDIATE 'delete from ' || p_TableName || ' where ' || p_Condition || ' and rownum <= :rn' USING p_Count;
     if SQL%NOTFOUND thenexit; elsen_delete := n_delete + SQL%ROWCOUNT;
end if;
 commit;
end loop;
 commit;
  DBMS_OUTPUT.PUT_LINE('Finished!'); DBMS_OUTPUT.PUT_LINE('Totally ' || to_char(n_delete) || ' records deleted!');
end;


以下是删除过程及时间:

SQL> create or replace procedure delBigTab
  2  (
  3    p_TableName       in    varchar2,
  4    p_Condition       in    varchar2,
  5    p_Count        in    varchar2
  6  )
  7  as
  8   pragma autonomous_transaction;
  9   n_delete number:=0;
 10  begin
 11   while 1=1 loop
 12     EXECUTE IMMEDIATE
 13       'delete from '||p_TableName||' where '||p_Condition||' and rownum <= :rn'
 14     USING p_Count;
 15     if SQL%NOTFOUND then
 16        exit;
 17     else
 18              n_delete:=n_delete + SQL%ROWCOUNT;
 19     end if;
 20     commit;
 21   end loop;
 22   commit;
 23   DBMS_OUTPUT.PUT_LINE('Finished!');
 24   DBMS_OUTPUT.PUT_LINE('Totally '||to_char(n_delete)||' records deleted!');
 25  end;
 26  /

Procedure created.

SQL> set timing on
SQL> select min(NUMDLFLOGGUID) from HS_DLF_DOWNLOG_HISTORY;

MIN(NUMDLFLOGGUID)
------------------
          11000000

Elapsed: 00:00:00.23
SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11100000','10000');

PL/SQL procedure successfully completed.

Elapsed: 00:00:18.54
SQL> select min(NUMDLFLOGGUID) from HS_DLF_DOWNLOG_HISTORY;

MIN(NUMDLFLOGGUID)
------------------
          11100000

Elapsed: 00:00:00.18
SQL> set serveroutput on
SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11200000','10000');
Finished!
Totally 96936 records deleted!

PL/SQL procedure successfully completed.

Elapsed: 00:00:18.61
10万记录大约19s

SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11300000','10000');
Finished!
Totally 100000 records deleted!

PL/SQL procedure successfully completed.

Elapsed: 00:00:18.62
SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 11400000','10000');
Finished!
Totally 100000 records deleted!

PL/SQL procedure successfully completed.

Elapsed: 00:00:18.85
SQL> 
SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 13000000','10000');
Finished!
Totally 1000000 records deleted!

PL/SQL procedure successfully completed.

Elapsed: 00:03:13.87

100万记录大约3分钟
SQL> exec delBigTab('HS_DLF_DOWNLOG_HISTORY','NUMDLFLOGGUID < 20000000','10000');
 
Finished!
Totally 6999977 records deleted!

PL/SQL procedure successfully completed.

Elapsed: 00:27:24.69
700万大约27分钟

以上过程仅供参考.

 

你可能感兴趣的:(sql%rowcount)