如何有條件的分段刪除資料表中的記錄

來源:互聯網
上載者:User
如何有條件的分段刪除資料表中的記錄作者:eygle出處:http://blog.eygle.com日期:February 22, 2005

« 自己動手,豐衣足食 | Blog首頁

有時候我們需要分配刪除資料表的一些記錄,分批提交以減少對於Undo的使用,本文提供一個簡單的預存程序用於實現該邏輯。
你可以根據你的需要進行適當調整,本例僅供參考:

SQL> create table test as select * from dba_objects;Table created.SQL> create or replace procedure deleteTab  2  /**  3   ** Usage: run the script to create the proc deleteTab  4   **        in SQL*PLUS, type "exec deleteTab('Foo','ID>=1000000','3000');"  5   **        to delete the records in the table "Foo", commit per 3000 records.  6   **        7   **/  8  (  9    p_TableName    in    varchar2,    -- The TableName which you want to delete from 10    p_Condition    in    varchar2,    -- Delete condition, such as "id>=100000" 11    p_Count        in    varchar2     -- Commit after delete How many records 12  ) 13  as 14   pragma autonomous_transaction; 15   n_delete number:=0; 16  begin 17   while 1=1 loop 18     EXECUTE IMMEDIATE 19       'delete from '||p_TableName||' where '||p_Condition||' and rownum <= :rn' 20     USING p_Count; 21     if SQL%NOTFOUND then 22     exit; 23     else 24          n_delete:=n_delete + SQL%ROWCOUNT; 25     end if; 26     commit; 27   end loop; 28   commit; 29   DBMS_OUTPUT.PUT_LINE('Finished!');  30   DBMS_OUTPUT.PUT_LINE('Totally '||to_char(n_delete)||' records deleted!'); 31  end; 32  /Procedure created.SQL> insert into test select * from dba_objects;6374 rows created.SQL> /6374 rows created.SQL> /6374 rows created.SQL> commit;Commit complete.SQL> exec deleteTab('TEST','object_id >0','3000')Finished!Totally 19107 records deleted!PL/SQL procedure successfully completed.

很簡單,但是想來也有人可以用到。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.