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

末鹿安然 提交于 2019-12-22 23:24:09

问题


I'm executing a statement like

DELETE FROM USER WHERE USER_ID=1;

In SQLDeveloper.

As the user is referenced in many tables (for example the user has an order, settings, ...) we activated ON DELETE CASCADE so that we do not have to delete each and every row manually. However while developing we are interested to know how many rows and and in which tables are "automatically" deleted by cascade delete.

Is there any way to find this out. Either by SQL-Statement, directly in sqldeveloper from a logfile or any other idea?


回答1:


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


来源:https://stackoverflow.com/questions/14439097/oracle-delete-statement-how-many-rows-have-been-deleted-by-cascade-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!