Need Help with After Trigger [message #583651] |
Thu, 02 May 2013 13:31  |
 |
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   |
 |
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 #583742 is a reply to message #583664] |
Fri, 03 May 2013 08:59  |
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.
|
|
|