Home » SQL & PL/SQL » SQL & PL/SQL » Trigger to stop deletion of row
Trigger to stop deletion of row [message #2337] Mon, 08 July 2002 07:39 Go to next message
Anna
Messages: 30
Registered: March 2001
Member
I have a table with columns id and parent_id

I want a trigger that will stop the deletion of a row
if it's id is listed as a parent_id in any other row.

How would I do this?
Re: Trigger to stop deletion of row [message #2339 is a reply to message #2337] Mon, 08 July 2002 08:05 Go to previous messageGo to next message
K.SREENIVASAN
Messages: 110
Registered: January 2001
Location: banglore
Senior Member
SIR,

CREATE OR REPLACE PROCEDURE TRIEXAMPLE
BEFORE DELETE ON TABLE1
ON EACH ROW
I NUMBER;
BEGIN
I:=0;
SELECT COUNT(*) INTO I FROM TABLE1 WHERE ID=
(SELECT A.ID FROM TABLE1 A,TABLE1 B WHERE A.ID=B.PARENT_ID AND A.ID=OLD.ID);
IF I>2 THEN
RAISE_APPLICATION_ERROR(200000,'CAN'T DELETE THE ROW');
END IF;
END;
/

K.SREENIVASAN
Re: Trigger to stop deletion of row [message #2342 is a reply to message #2337] Mon, 08 July 2002 09:55 Go to previous message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
To handle this in triggers and to avoid the mutating table error, you need to build a collection of three triggers - a before statement trigger, an after row trigger, and an after statement trigger. This requirement is best met (and easier handled) through a foreign key constraint. See my posting in the PL/SQL forum in response to your question.
Previous Topic: Trigger Beginner
Next Topic: Record Counting
Goto Forum:
  


Current Time: Fri Apr 26 18:25:14 CDT 2024