Home » SQL & PL/SQL » SQL & PL/SQL » Using SQL%Rowcount inside trigger
Using SQL%Rowcount inside trigger [message #41360] Sat, 21 December 2002 05:13 Go to next message
prashant
Messages: 122
Registered: September 2000
Senior Member
Hi all

I am facing a small problem here. What I want to do is that whenever somebody
issues an update or delete command, if the number of rows afftected is
more than say 100 then i want that transaction to fail.

In cases of deletes what i am doing is , I have a packaged variable and
I have two triggers, one before delete, in which i do a select count(*)
from the table and store it in the packaged variable and
an after delete, in which i again do a select count(*) and then compare the
2 counts and if the difference is more than 100 then i use
Raise_application_error. But i dont want to use this method cos there can be a
possibility of the the second count showing incorrect result because of some other
committed transactions.

Also this method cannot be used for updates.
Now i have tried to use the implicit cursor attribute SQL%Rowcount inside the trigger
body of the "after update" trigger but it is always null.

Can somebody help me out here in finding out the number of rows affected and use it inside
the trigger.

Thanks in advance
Re: Using SQL%Rowcount inside trigger [message #41361 is a reply to message #41360] Sat, 21 December 2002 06:25 Go to previous messageGo to next message
Barbara Boehmer
Messages: 9094
Registered: November 2002
Location: California, USA
Senior Member
CREATE OR REPLACE PACKAGE state_pkg
AS
  v_rows_affected NUMBER;
END state_pkg;
/

CREATE OR REPLACE TRIGGER your_table_bud
  BEFORE UPDATE OR DELETE ON your_table
BEGIN
  state_pkg.v_rows_affected := 0;
END your_table_bud;
/

CREATE OR REPLACE TRIGGER your_table_audr
  AFTER UPDATE OR DELETE ON your_table
  FOR EACH ROW
BEGIN
  state_pkg.v_rows_affected := state_pkg.v_rows_affected + 1;
END your_table_audr;
/

CREATE OR REPLACE TRIGGER your_table_aud
  AFTER UPDATE OR DELETE ON your_table
BEGIN
  IF state_pkg.v_rows_affected > 100
  THEN
    RAISE_APPLICATION_ERROR 
      (-20001, 
       'You are not allowed to perform an update or delete on more than 100 rows.');
  END IF;
END your_table_aud;
/
Thanks A Lot !!!! [message #41362 is a reply to message #41360] Sat, 21 December 2002 07:36 Go to previous message
prashant
Messages: 122
Registered: September 2000
Senior Member
No Message Body
Previous Topic: Accept Command
Next Topic: Very newbie requires help
Goto Forum:
  


Current Time: Fri May 17 07:43:29 CDT 2024