3

I have a statement level trigger that fires whenever INSERT UPDATE or DELETE operations are performed on a table (called customers). I want to display a message (to DBMS_OUTPUT) containing the number of rows that were inserted/updated/deleted.

I just want one message for each triggering statement, eg '4 rows were inserted into customers table'.

How can I access the number of rows that are affected by the triggering statement from INSIDE the trigger declaration, ie XXX in the code below:

CREATE OR REPLACE TRIGGER customer_changes_trigger_2
AFTER INSERT OR UPDATE OR DELETE ON customers

DECLARE
v_operation   VARCHAR(10);
v_number_rows NUMBER;


BEGIN

v_number := XXX;

IF INSERTING THEN
   v_operation := 'inserted';
END IF;

IF UPDATING THEN
   v_operation := 'updated';
END IF;

IF DELETING THEN
   v_operation := 'deleted';
END IF;

DBMS_OUTPUT.PUT_LINE
          (v_number_rows|| ' rows were ' || v_operation || ' from customers.');
END;

Can't find anything in the documentation, any help appreciated!

5
  • Why on earth do you want to dbms_output? Are you going to be there to check each time?
    – Ben
    Jan 7, 2012 at 14:37
  • at the moment for debugging, later to insert into a table.
    – dav83
    Jan 7, 2012 at 14:39
  • why not just change the trigger to a for each row and insert into your tracking table then have a separate query to sum them?
    – Ben
    Jan 7, 2012 at 15:18
  • 1
    thanks for your input - I guess there are many ways to do it. However I am interested in knowing if the question above has an answer, which is why I asked it.
    – dav83
    Jan 7, 2012 at 15:23
  • I don't know of one, which is why I commented :-).
    – Ben
    Jan 7, 2012 at 15:27

2 Answers 2

4

One way is to use a global variable to track the number of rows as there is no other way to get the row count from a statement level trigger. You would then need three triggers... one statement level to initialise the variable before the statement is run, one row level to add one to the variable for each row, one statement level to use the row count however you wish. First, set up the variable and a few procedures to help it:

create or replace package PKG_ROWCOUNT is
  NUMROWS   number;

  procedure INIT_ROWCOUNT;

  procedure ADD_ONE;

  function GET_ROWCOUNT
    return number;
end PKG_ROWCOUNT;
/

create or replace package body PKG_ROWCOUNT as
  procedure INIT_ROWCOUNT is
  begin
    NUMROWS := 0;
  end;

  procedure ADD_ONE is
  begin
    NUMROWS := Nvl(NUMROWS, 0) + 1;
  end;

  function GET_ROWCOUNT
    return number is
  begin
    return NUMROWS;
  end;
end PKG_ROWCOUNT;
/

The first trigger to initialise the variable:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_1
  before insert or update or delete
  on CUSTOMERS
begin
  PKG_ROWCOUNT.INIT_ROWCOUNT;
end;

The second to update per row:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_2
  after insert or update or delete
  on CUSTOMERS
  for each row
begin
  PKG_ROWCOUNT.ADD_ONE;
end;
/

The third to display the total:

create or replace trigger CUSTOMER_CHANGES_TRIGGER_3
  after insert or update or delete
  on CUSTOMERS
begin
   Dbms_output.
   PUT_LINE(PKG_ROWCOUNT.GET_ROWCOUNT || ' rows were affected.');
end;
2
  • 2
    This may give incorrect results in the case of statement restart. If the row trigger was a before trigger, I would know it is a problem. Being an after trigger, I don't know if restart is a possibility or not. For more information on statement restart see: tkyte.blogspot.com/search?q=statement+restart Jan 8, 2012 at 22:56
  • 1
    This is much easier to do now since Oracle 11g, which introduced compound triggers. In the before statement initialize a rowCount variable. In the for each row section, increment that variable. In the after statement section, you'll have access to the value. So you don't need 3 triggers, all logic can live in one compound trigger.
    – mancini0
    Feb 23, 2017 at 19:25
-2

I'm not 100$ sure if it's available inside AFTER trigger body, but you can try examining sql%rowcount

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.