Oracle Delete Statement: how many rows have been deleted by cascade delete

跟風遠走 提交于 2019-12-06 11:55:37

whilst this is not possible with sql%rowcount, it is possible if you write trigger code, but this means that you need a trigger on all tables you want to monitor. Also triggers would slow down operations a bit.

e.g.:

SQL> select * from one;

        ID
----------
         1
         2

SQL> select * from child_of_one;

        ID       O_ID
---------- ----------
         1          1
         2          1
         3          1
         4          2
         5          2
         6          2
         7          2
         8          2

we want a package spec to hold an array of tables + counts:

SQL> create or replace package foo
  2  as
  3    type rowcount_tab is table of pls_integer index by varchar2(30);
  4    t_rowcount rowcount_tab;
  5  end foo;
  6  /

Package created.

we want a trigger on the top level table to reset these counts to zero:

SQL> create or replace trigger one_biud
  2  before insert or update or delete
  3  on one
  4  declare
  5  begin
  6    foo.t_rowcount.delete;
  7  end;
  8  /

Trigger created.

this assumes that you're only interested in the array with a delete from the top level table. if not, you'd want a trigger on each table with foo.t_rowcount.delete('TABLE_NAME') instead.

now a after for each row trigger on each table of interest to set the arrays:

SQL> create or replace trigger one_aiudfer
  2  after insert or update or delete
  3  on one
  4  for each row
  5  declare
  6  begin
  7    if (foo.t_rowcount.exists('ONE'))
  8    then
  9      foo.t_rowcount('ONE') := nvl(foo.t_rowcount('ONE'), 0)+1;
 10    else
 11      foo.t_rowcount('ONE') := 1;
 12    end if;
 13  end;
 14  /

Trigger created.

SQL> create or replace trigger child_of_one_aiudfer
  2  after insert or update or delete
  3  on child_of_one
  4  for each row
  5  declare
  6  begin
  7    if (foo.t_rowcount.exists('CHILD_OF_ONE'))
  8    then
  9      foo.t_rowcount('CHILD_OF_ONE') := nvl(foo.t_rowcount('CHILD_OF_ONE'), 0)+1;
 10    else
 11      foo.t_rowcount('CHILD_OF_ONE') := 1;
 12    end if;
 13  end;
 14  /

Trigger created.

now when we delete or whatever:

SQL> delete from one where id = 1;

1 row deleted.

SQL> declare
  2    v_table varchar2(30);
  3  begin
  4    v_table := foo.t_rowcount.first;
  5    loop
  6       exit when v_table is null;
  7             dbms_output.put_line(v_table || ' ' || foo.t_rowcount(v_table) || ' rows');
  8             v_table := foo.t_rowcount.next(v_table);
  9     end loop;
 10  end;
 11  /
CHILD_OF_ONE 3 rows
ONE 1 rows

PL/SQL procedure successfully completed.

SQL> delete from one where id = 2;

1 row deleted.

SQL> declare
  2    v_table varchar2(30);
  3  begin
  4    v_table := foo.t_rowcount.first;
  5    loop
  6       exit when v_table is null;
  7             dbms_output.put_line(v_table || ' ' || foo.t_rowcount(v_table) || ' rows');
  8             v_table := foo.t_rowcount.next(v_table);
  9     end loop;
 10  end;
 11  /
CHILD_OF_ONE 5 rows
ONE 1 rows
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!