Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Pernyataan Hapus Oracle:berapa banyak baris yang telah dihapus oleh penghapusan kaskade

sementara ini tidak mungkin dengan sql%rowcount, dimungkinkan jika Anda menulis kode pemicu, tetapi ini berarti Anda memerlukan pemicu di semua tabel yang ingin Anda pantau. Pemicu juga akan sedikit memperlambat operasi.

misalnya:

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

kami ingin spesifikasi paket untuk menampung array tabel + jumlah:

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.

kami ingin pemicu di tabel tingkat atas menyetel ulang jumlah ini ke nol:

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.

ini mengasumsikan bahwa Anda hanya tertarik pada array dengan penghapusan dari tabel tingkat atas. jika tidak, Anda ingin pemicu di setiap tabel dengan foo.t_rowcount.delete('TABLE_NAME') sebagai gantinya.

sekarang setelah untuk setiap baris memicu pada setiap tabel yang menarik untuk mengatur array:

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.

sekarang ketika kami menghapus atau apa pun:

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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Kesalahan saat mencoba mengimpor driver Oracle jdbc7 dengan Maven

  2. Bagaimana cara menyimpan data unicode ke oracle?

  3. Apa yang setara dengan SQL Server APPLY di Oracle?

  4. Ubah pecahan ke desimal

  5. batasan integritas oracle