Home » SQL & PL/SQL » SQL & PL/SQL » Need Help with After Trigger (Oracle 11g, windows 7)
Need Help with After Trigger [message #583651] Thu, 02 May 2013 13:31 Go to next message
Bazil
Messages: 3
Registered: May 2013
Junior Member
Greetings!

I have a next trouble:

My table(CARD_FILE) have a 3 columns: IDs, Surname, Dept_code
Have to created After trigger for updating table on cloumn Dept_code .
If user change Dept_code for somebody, trigger have to set new Dept_code for everybody whose have same Dept_code.

At first I faced a trouble of mutating table. So, I tried to use Autonomous transactions, but result wasn`t fine: 00060. 00000 - "deadlock detected while waiting for resource"


Test Table
CREATE TABLE CARD_FILE
          (IDs        NUMBER(3),
           Surname    VARCHAR2(15),
           Dept_code  NUMBER(3));
           
INSERT INTO CARD_FILE
VALUES (1, 'Smith', 10);
INSERT INTO CARD_FILE
VALUES (2, 'Tomma', 10);
INSERT INTO CARD_FILE
VALUES (3, 'Gerk', 20);
INSERT INTO CARD_FILE
VALUES (4, 'Krake', 20;
INSERT INTO CARD_FILE
VALUES (6, 'Dugal', 30);
INSERT INTO CARD_FILE
VALUES (7, 'Kate', 30);
INSERT INTO CARD_FILE
VALUES (8, 'Dugal', 30);
INSERT INTO CARD_FILE
VALUES (9, 'Dugal', 50);
INSERT INTO CARD_FILE
VALUES (10, 'Dugal', 50);



My attempt (trigger):
CREATE OR REPLACE
TRIGGER mod_dept 
AFTER UPDATE OF dept_code
ON card_file FOR EACH ROW
DECLARE pragma autonomous_transaction;
dept_code NUMBER(4);
BEGIN
  IF :NEW.dept_code<>:OLD.dept_code THEN
  BEGIN UPDATE card_file 
  SET dept_code=:NEW.dept_code
  WHERE dept_code=:OLD.dept_code; 
  END;
  END IF;
  COMMIT;
END;



Try to make update:
UPDATE CARD_FILE SET Dept_code=40
WHERE Surname='Oldmann';


Result:
00060. 00000 - "deadlock detected while waiting for resource"
*Cause: Transactions deadlocked one another while waiting for resources.
*Action: Look at the trace file to see the transactions and resources
involved. Retry if necessary.


I`ll be really thankful if you help me with that trigger.

PS: sorry for my bad English.

Re: Need Help with After Trigger [message #583652 is a reply to message #583651] Thu, 02 May 2013 13:55 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
You do not need a trigger to do what you want:
SQL> SELECT * FROM CARD_FILE
  2  WHERE Surname='Tomma';

       IDS SURNAME                                        DEPT_CODE
---------- --------------------------------------------- ----------
         2 Tomma                                                 10

SQL> UPDATE CARD_FILE SET Dept_code=40
  2  WHERE Surname='Tomma';

1 row updated.

SQL> SELECT * FROM CARD_FILE
  2  WHERE Surname='Tomma';

       IDS SURNAME                                        DEPT_CODE
---------- --------------------------------------------- ----------
         2 Tomma                                                 40

[Updated on: Thu, 02 May 2013 14:14] by Moderator

Report message to a moderator

Re: Need Help with After Trigger [message #583653 is a reply to message #583652] Thu, 02 May 2013 14:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
This is a stupid answer. Razz

Regards
Michel

[Updated on: Thu, 02 May 2013 14:20]

Report message to a moderator

Re: Need Help with After Trigger [message #583655 is a reply to message #583651] Thu, 02 May 2013 14:19 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
NEVER EVER try to workaround a mutating error with an autonomous transaction.
If you have this error this is to prevent you from doing something really wrong that will lead to inconsistent data.
Try to understand the error, YOUR error not Oracle one, not an Oracle limit, a fundamental logical error.

The only CORRECT thing you can do is to make this kind of updates in a procedure that will UPDATE all the rows with the same old dept as a whole and forbid any update of deptno that does not come from this procedure (this can be done in a trigger).

Regards
Michel
Re: Need Help with After Trigger [message #583656 is a reply to message #583652] Thu, 02 May 2013 14:20 Go to previous messageGo to next message
Bazil
Messages: 3
Registered: May 2013
Junior Member
Maybe my description was not correct, but I mean next:

Tomma and Smith has DEPT_CODE=10
User changes Tomma`s DEPT_CODE, after it trigger changes Smith`s DEPT_CODE, becouse they have same DEPT_CODE(10)...

Anyway thanks you =)

[Updated on: Thu, 02 May 2013 14:22]

Report message to a moderator

Re: Need Help with After Trigger [message #583658 is a reply to message #583656] Thu, 02 May 2013 14:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
See my previous post (posted when you were writing yours and maybe you didn't see it).
And your post and question was clear. Smile

Regards
Michel

[Updated on: Thu, 02 May 2013 14:23]

Report message to a moderator

Re: Need Help with After Trigger [message #583659 is a reply to message #583655] Thu, 02 May 2013 14:31 Go to previous messageGo to next message
Bazil
Messages: 3
Registered: May 2013
Junior Member
Michel Cadot wrote on Thu, 02 May 2013 23:19

The only CORRECT thing you can do is to make this kind of updates in a procedure that will UPDATE all the rows with the same old dept as a whole


It means to use INSTEAD TRIGGER. But what you say about intermediate table (nested or varray) in trigger? Maybe I can copy strings with same DEPT_CODE, UPDATE their and substitute OLD data in CARD_FILE by data from intermediate table.
Re: Need Help with After Trigger [message #583664 is a reply to message #583659] Thu, 02 May 2013 15:33 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
You didn't understand what I said.
Forget the trigger to do what you want.
Use a procedure to do it and call this procedure to make any update on deptno.
Then add a trigger to forbid any update of deptno that does not come from this procedure.
The trigger here makes only one thing: forbid the unwanted access.

And I didn't talk any thing about an intermediate table.

Regards
Michel
Re: Need Help with After Trigger [message #583742 is a reply to message #583664] Fri, 03 May 2013 08:59 Go to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
There is one other issue!!! Just because one employee goes to another department why would you change everyone else in the department to follow them. I can understand changing a depart number in the DEPT table and having the children change too but what you are trying to do makes no sense.
Previous Topic: Select data from 2 tables and insert into another table
Next Topic: SP2-0027: Input is too long (> 2499 characters)
Goto Forum:
  


Current Time: Thu Aug 21 15:59:49 CDT 2025