Mutating error in trigger
Hi ,ihave created this trigger.its showing the mutating error as its selecting and updating the same table.So hw can we modify thsi trigger so that it worksd properly.
CREATE OR REPLACE TRIGGER resolutionimpact
AFTER INSERT OR UPDATE
ON PROBLEMS
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
newcause VARCHAR2(100);
oldcause VARCHAR2(10);
BEGIN
oldcause:=:OLD.PROM_NCCCAUSE;
newcause:=:NEW.PROM_NCCCAUSE;
IF newcause='100' THEN
UPDATE PROBLEMS SET PROM_IMPACT='CA_TCL FLP reset';
--PROM_IMPACT='CA_TCL FLP reset';
END IF;
END;
No actually I am udating single row.Actually the value is being entered from the oracle form from front end and no matter whatever value we enter for prom_impact from front end it should overwrite that value with fixed value when we press the save button .Also we cannot do anything at form level as we have fmx and nt the fmb.So have to do it through trigger only.Also there is another trigger running on same table which fetches the value from problem table and inserts it into another table.So what can be done to solve the issue
Similar Messages
-
URGENT : Trigger with Mutating Error
Hi,
I'm trying to write a trigger on a Table ROOM_BLOCK to update a value in table ROOM_OOI_OOS.
As given below, which works perfectly.
BEGIN
IF INSERTING THEN
IF NVL(:NEW.ROOM_OOI_OOS_ID,0) <> 0 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)+1
WHERE ROOM_OOI_OOS_ID = :NEW.ROOM_OOI_OOS_ID;
END IF;
ELSIF UPDATING THEN
IF NVL(:OLD.ROOM_OOI_OOS_ID,0) <> 0 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)-1
WHERE ROOM_OOI_OOS_ID = :OLD.ROOM_OOI_OOS_ID;
END IF;
IF NVL(:NEW.ROOM_OOI_OOS_ID,0) <> 0 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)+1
WHERE ROOM_OOI_OOS_ID = :NEW.ROOM_OOI_OOS_ID;
END IF;
ELSE
IF NVL(:OLD.ROOM_OOI_OOS_ID,0) <> 0 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)-1
WHERE ROOM_OOI_OOS_ID = :OLD.ROOM_OOI_OOS_ID;
END IF;
END IF;
END;
But when I add a select statement. the trigger seems to fail with a mutating error ORA-04091
and Error at Line which executes the statement immidiate ORA-04088.
DECLARE
stmt VARCHAR2(500);
vcount INTEGER;
BEGIN
IF INSERTING THEN
--INSERT INTO SHRUTI_TEST (DESCR) VALUES ('Inserting');
IF NVL(:NEW.ROOM_OOI_OOS_ID,0) <> 0 THEN
stmt := 'Select count(*) from ROOM_BLOCK ' ||
' WHERE ROOM_OOI_OOS_ID = ' || :NEW.ROOM_OOI_OOS_ID ||
' AND ROOM_NO = ''' || :NEW.ROOM_NO || ''' ';
EXECUTE IMMEDIATE stmt INTO vcount;
IF vcount = 1 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)+1
WHERE ROOM_OOI_OOS_ID = :NEW.ROOM_OOI_OOS_ID;
END IF;
END IF;
ELSIF UPDATING THEN
IF NVL(:OLD.ROOM_OOI_OOS_ID,0) <> 0 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)-1
WHERE ROOM_OOI_OOS_ID = :OLD.ROOM_OOI_OOS_ID;
END IF;
IF NVL(:NEW.ROOM_OOI_OOS_ID,0) <> 0 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)+1
WHERE ROOM_OOI_OOS_ID = :NEW.ROOM_OOI_OOS_ID;
END IF;
ELSE
IF NVL(:OLD.ROOM_OOI_OOS_ID,0) <> 0 THEN
UPDATE ROOM_OOI_OOS SET NO_OF_ROOMS = NVL(NO_OF_ROOMS,0)-1
WHERE ROOM_OOI_OOS_ID = :OLD.ROOM_OOI_OOS_ID;
END IF;
END IF;
END;
Please tell me how to solve this problem...
Regards
Mike.Thank you for your reply to my posting.
I researched and used your recommendation. I was
wondering if you can help me resolve the following two
problems.
Table kassa1 has NAME and AGE columns. Table Kassa2
has one column Age.
I have created the following trigger. It keeps
raising a ORA-06519 : Active autonomous transaction
detected and rolled back error.
Create or replace trigger Name_Age_Row after insert or
update on Kassa1
For each row
Declare
Total_Age VARCHAR(8);
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
Select SUM(Age) into Total_Age from Kassa1;
insert into kassa2 values(Total_aAge);
End;
The following trigger also generates ORA-00036:
Maximum number of recursive SQL levels(50) exceeded
error.
Create or replace trigger Name_Age_Row after insert or
update on Kassa1
For each row
Declare
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
insert into kassa1 values('newname', 'newage');
End; -
Mutating error in my trigger code
Hi,
Could anyone please help me out to fix the mutating error:
I have 2 tables z_errorpayment and z_errorcorrected.
If the status=2 in z_errorpayment table then a row has to be inserted in z_errorcorrected table and the same row has to be deleted from the 1st table i.e z_errorpayment.As it is retriving from the same table and deleting the same table am getting mutating error.
Below is the code.
CREATE OR REPLACE TRIGGER row_trigger
AFTER UPDATE
ON z_errorpayment
FOR EACH ROW
WHEN (NEW.status = 2)
BEGIN
INSERT INTO z_errorcorrected
(mtnl_error_payment_id, file_name, entry_date,
centre_code, service_code, instrument_flag,
instrument_no, subs_no, instrument_dt,
payment_mode, payment_dt, surchrg,
telephone_no, cheque_number, cheque_dt,
bank_code, pay_amount, receipt_no,
terminal_id, crtn_by, crtn_dt, remarks,
status, external_trans_id, transfer_date,
gl_uploaded_ind_code, task_queue_id,
user_name, old_payment_id, new_payment_id,
payment_type
VALUES (:NEW.mtnl_error_payment_id, :NEW.file_name, :NEW.entry_date,
:NEW.centre_code, :NEW.service_code, :NEW.instrument_flag,
:NEW.instrument_no, :NEW.subs_no, :NEW.instrument_dt,
:NEW.payment_mode, :NEW.payment_dt, :NEW.surchrg,
:NEW.telephone_no, :NEW.cheque_number, :NEW.cheque_dt,
:NEW.bank_code, :NEW.pay_amount, :NEW.receipt_no,
:NEW.terminal_id, :NEW.crtn_by, :NEW.crtn_dt, :NEW.remarks,
:NEW.status, :NEW.external_trans_id, :NEW.transfer_date,
:NEW.gl_uploaded_ind_code, :NEW.task_queue_id,
:NEW.user_name, :NEW.old_payment_id, :NEW.new_payment_id,
:NEW.payment_type
DELETE FROM z_errorpayment
WHERE new_payment_id = :NEW.new_payment_id;
END;
so I thought of using a view inorder to get rid of the mutating error.
But donno how to code it.
could anyone please help me out.
Regards,
RupaBy using a view i wrote the below code:
CREATE OR REPLACE TRIGGER row_trigger
INSTEAD OF UPDATE
ON Z_ErrorView
FOR EACH ROW
DECLARE
X INTEGER;
BEGIN
SELECT STATUS into X FROM z_errorpayment WHERE new_payment_id = :NEW.new_payment_id;
IF X=2 THEN
INSERT INTO z_errorcorrected
(mtnl_error_payment_id, file_name, entry_date,
centre_code, service_code, instrument_flag,
instrument_no, subs_no, instrument_dt,
payment_mode, payment_dt, surchrg,
telephone_no, cheque_number, cheque_dt,
bank_code, pay_amount, receipt_no,
terminal_id, crtn_by, crtn_dt, remarks,
status, external_trans_id, transfer_date,
gl_uploaded_ind_code, task_queue_id,
user_name, old_payment_id, new_payment_id,
payment_type
VALUES (:NEW.mtnl_error_payment_id, :NEW.file_name, :NEW.entry_date,
:NEW.centre_code, :NEW.service_code, :NEW.instrument_flag,
:NEW.instrument_no, :NEW.subs_no, :NEW.instrument_dt,
:NEW.payment_mode, :NEW.payment_dt, :NEW.surchrg,
:NEW.telephone_no, :NEW.cheque_number, :NEW.cheque_dt,
:NEW.bank_code, :NEW.pay_amount, :NEW.receipt_no,
:NEW.terminal_id, :NEW.crtn_by, :NEW.crtn_dt, :NEW.remarks,
:NEW.status, :NEW.external_trans_id, :NEW.transfer_date,
:NEW.gl_uploaded_ind_code, :NEW.task_queue_id,
:NEW.user_name, :NEW.old_payment_id, :NEW.new_payment_id,
:NEW.payment_type
END IF;
DELETE FROM z_errorpayment
WHERE new_payment_id = :NEW.new_payment_id;
END; -
Help with Mutating Error / Trigger / APEX_MAIL.SEND
Hello:
I am trying to get the apex_mail.send function working when a user submits a new record. I've taken the code from the canned Issue Tracker application code and tried to retrofit it to my tables. However I keep receiving a mutating error on line 11 (line 11 bolded below):
-- Create a trigger that will send notification after
-- the submission of each new idea on the IDEAS table.
create or replace trigger IDEAS_EMAIL
AFTER
insert or update on IDEAS
for each row
begin
IF (INSERTING AND :new.manager_name IS NOT NULL)
OR
(UPDATING AND (:old.manager_name IS NULL OR :new.manager_name != :old.manager_name) AND :new.manager_name IS NOT NULL) THEN
FOR c1 IN
(SELECT fname, lname, email
FROM ideas_users
WHERE fname||' '||lname = :new.manager_name)
LOOP
IF c1.email IS NOT NULL THEN
FOR c2 IN
(SELECT MANAGER_NAME, FULL_TEXT, SUMMARY, ID, SUBMITTED_BY, SUB_DATE
FROM IDEAS
WHERE ID = :new.ID)
LOOP
APEX_MAIL.SEND(
p_to => c1.email,
p_from => c1.email,
p_body =>
'This is a test message. ' ||chr(10)||
p_subj => 'test email subject');
END LOOP;
END IF;
END LOOP;
END IF;
end;
I've also tried setting the following line to the primary key field in the user table and inserting that value into the main table but I still get the same error message.
WHERE fname||' '||lname = :new.manager_name)
Thanks in advance for any help.Let me get this straight..
You are inserting a row into a table, and when you insert the row, you are going to send an e-mail t o the manager who owns this row. In the data you are inserting, is the e-mail address of the manager, right?
David has provided the answer for you.. Use the :NEW.Email value instead of creating a cursor and all that.. Unless you have a need to e-mail multiple people of the record insert..
Thank you,
Tony Miller
Webster, TX
On the road of life...There are 'windshields', and there are 'bugs'
(splat!)
"Squeegees Wanted" -
How to avoid mutating error when insert or update record
Hi ,
I have one transaction table which is having some detail record under one transaction number, after the one transaction number is over by insert or update, i
want to check the total amounts of one flag should be matched on same table if it is not then give error message. But i am getting mutating error on insert or update event trigger on statement level trigger on above table.
Is there any other way to avoid mutating error to solve the above problem or some temp table concepts to be used. help me its urgent.
Thanks in advance,
Sachin Khaladkar
PuneSachin, here's as short of an example as I could come up with on the fly. The sample data is ficticious and for example only.
Let's say I need to keep a table of items by category and my business rule states that the items in the table within each category must total to 100% at all times. So I want to insert rows and then make sure any category added sums to 100% or I will rollback the transation. I can't sum the rows in a row-level trigger because I'd have to query the table and it is mutating (in the middle of being changed by a transaction). Even if I could query it while it is mutating, there may be multiple rows in a category with not all yet inserted, so checking the sum after each row is not useful.
So here I will create;
1. the item table
2. a package to hold my record collection (associative array) for the trigger code (the category is used as a key to the array; if I insert 3 rows for a given category, I only need to sum that category once, right?
3. a before statement trigger to initialize the record collection (since package variables hang around for the entire database session, I need to clear the array before the start of every DML (INSERT in this case) statement against the item table)
4. a before row trigger to collect categories being inserted
5. an after statement trigger to validate my business rule
I then insert some sample data so you can see how it works. Let me know if you have any questions about this.
SQL> CREATE TABLE item_t
2 (category NUMBER(2) NOT NULL
3 ,item_code VARCHAR2(2) NOT NULL
4 ,pct NUMBER(3,2) NOT NULL);
Table created.
SQL>
SQL> CREATE OR REPLACE PACKAGE trg_pkg IS
2 TYPE t_item_typ IS TABLE OF item_t.category%TYPE
3 INDEX BY PLS_INTEGER;
4 t_item t_item_typ;
5 t_empty_item t_item_typ;
6 END trg_pkg;
7 /
Package created.
SQL> SHOW ERRORS;
No errors.
SQL>
SQL> CREATE OR REPLACE TRIGGER item_bs_trg
2 BEFORE INSERT
3 ON item_t
4 BEGIN
5 DBMS_OUTPUT.put_line('Initializing...');
6 trg_pkg.t_item := trg_pkg.t_empty_item;
7 END item_bs_trg;
8 /
Trigger created.
SQL> SHOW ERRORS;
No errors.
SQL>
SQL> CREATE OR REPLACE TRIGGER item_br_trg
2 BEFORE INSERT
3 ON item_t
4 FOR EACH ROW
5 BEGIN
6 trg_pkg.t_item(:NEW.category) := :NEW.category;
7 DBMS_OUTPUT.put_line('Inserted Item for Category: '||:NEW.category);
8 END item_br_trg;
9 /
Trigger created.
SQL> SHOW ERRORS;
No errors.
SQL>
SQL> CREATE OR REPLACE TRIGGER item_as_trg
2 AFTER INSERT
3 ON item_t
4 DECLARE
5 CURSOR c_item (cp_category item_t.category%TYPE) IS
6 SELECT SUM(pct) pct
7 FROM item_t
8 WHERE category = cp_category;
9 BEGIN
10 DBMS_OUTPUT.put_line('Verifying...');
11 FOR i IN trg_pkg.t_item.FIRST..trg_pkg.t_item.LAST LOOP
12 DBMS_OUTPUT.put_line('Checking Category: '||trg_pkg.t_item(i));
13 FOR rec IN c_item(trg_pkg.t_item(i)) LOOP
14 IF rec.pct != 1 THEN
15 RAISE_APPLICATION_ERROR(-20001,'Category '||trg_pkg.t_item(i)||' total = '||rec.pct);
16 END IF;
17 END LOOP;
18 END LOOP;
19 END item_as_trg;
20 /
Trigger created.
SQL> SHOW ERRORS;
No errors.
SQL> INSERT INTO item_t
2 SELECT 1, 'AA', .3 FROM DUAL
3 UNION ALL
4 SELECT 2, 'AB', .6 FROM DUAL
5 UNION ALL
6 SELECT 1, 'AC', .2 FROM DUAL
7 UNION ALL
8 SELECT 3, 'AA', 1 FROM DUAL
9 UNION ALL
10 SELECT 1, 'AA', .5 FROM DUAL
11 UNION ALL
12 SELECT 2, 'AB', .4 FROM DUAL;
Initializing...
Inserted Item for Category: 1
Inserted Item for Category: 2
Inserted Item for Category: 1
Inserted Item for Category: 3
Inserted Item for Category: 1
Inserted Item for Category: 2
Verifying...
Checking Category: 1
Checking Category: 2
Checking Category: 3
6 rows created.
SQL>
SQL> SELECT * FROM item_t ORDER BY category, item_code, pct;
CATEGORY IT PCT
1 AA .3
1 AA .5
1 AC .2
2 AB .4
2 AB .6
3 AA 1
6 rows selected.
SQL>
SQL> INSERT INTO item_t
2 SELECT 4, 'AB', .5 FROM DUAL
3 UNION ALL
4 SELECT 5, 'AC', .2 FROM DUAL
5 UNION ALL
6 SELECT 5, 'AA', .5 FROM DUAL
7 UNION ALL
8 SELECT 4, 'AB', .5 FROM DUAL
9 UNION ALL
10 SELECT 4, 'AC', .4 FROM DUAL;
Initializing...
Inserted Item for Category: 4
Inserted Item for Category: 5
Inserted Item for Category: 5
Inserted Item for Category: 4
Inserted Item for Category: 4
Verifying...
Checking Category: 4
INSERT INTO item_t
ERROR at line 1:
ORA-20001: Category 4 total = 1.4
ORA-06512: at "PNOSKO.ITEM_AS_TRG", line 12
ORA-04088: error during execution of trigger 'PNOSKO.ITEM_AS_TRG'
SQL>
SQL> SELECT * FROM item_t ORDER BY category, item_code, pct;
CATEGORY IT PCT
1 AA .3
1 AA .5
1 AC .2
2 AB .4
2 AB .6
3 AA 1
6 rows selected.
SQL> -
Hi,
I have form in which we are creating parent child relation .The table structure is as follows
problems(table name)(prom_number,prom_relation,prom_impact,prom_relation,...)(column name).
I dont have the forms fmb file ,So whenwe create the relation 2 update statement gets executed first in problem table the prom_number which is parent in its problem_relation column its assigned as parent and the problem which is chilld in its column value assigne dare (prom_relation,prom_prom_number)(child,and the value or pro_number which is parent).
Now our requirement is that the value of prom_impact of child should be assigned to problem which is parent,So i have written a trigger which update the prom_impact column whcih gives mutation error.SO how can we solve this issue.The trigger i have written is given below.
The problem is that from form level the table is updated twice and in the trigger i try to update the row which is parent fault .howere its show mutation error.I even created trigger which shall commit the when from problem_rlation is cahnged to parent .so that in my trigger wheh i update the column prom_imapct field in trigger there should be no reeor.Still it given mutation error.
CREATE OR REPLACE TRIGGER parent_child_impact_trg
AFTER UPDATE /*OF prom_relation,prom_prom_number*/ ON PROBLEMS
REFERENCING NEW AS NEW
FOR EACH ROW
Version History:9.2.8.1
(format: version, date, developer, description)
1.0, 19-Dec-2008, Tanmoy Kr Moulik The "Impact" of Paren fault ticket
will be changed upon manual creation of Parent Child relation
Rules:
1.0 Upon manual creation of new Parent-Child fault relation, the 'Fault Impact' of
Child fault will be auto updated in the parent fault, if the parent fault impact matches
with the attached list(Non Serve Affeect or Threat) or the impact of Parent Fault is blank (null).
2.0 In case, the parent fault have fault impact matches with the attached list
(service affect), then its impact is not changed while making the relationship.
DECLARE
PRAGMA autonomous_transaction;
ecode NUMBER;
emesg VARCHAR2(200);
--BEGIN
-- NULL;
CURSOR c1--(v_prom_num NUMBER)
IS
SELECT PROM_IMPACT,prom_number FROM PROBLEMS WHERE prom_number=:NEW.prom_prom_number;
v_prom_imp VARCHAR2(30) DEFAULT NULL;
v_prom_imp_p1 NUMBER DEFAULT 0;
v_prom_imp_p2 NUMBER DEFAULT 0;
v_prom_num NUMBER;
BEGIN
IF :NEW.PROM_RELATION='CHILD' AND :NEW.PROM_PROM_NUMBER IS NOT NULL THEN
--dbms_output.put_line(1);
--INSERT INTO TEMP_PROM VALUES(:NEW.prom_number,:NEW.prom_prom_number,:NEW.prom_impact,:NEW.prom_relation);
--COMMIT;
OPEN c1;--(:NEW.prom_prom_number);
FETCH c1 INTO v_prom_imp,v_prom_num;
--INSERT INTO TEMP_PROM(PROM_NUM,PROM_IMPACT) VALUES(v_prom_num,v_prom_imp);
--COMMIT;
IF c1%NOTFOUND THEN
v_prom_imp:=NULL;
v_prom_num:=NULL;
END IF;
CLOSE c1;
v_prom_imp:=UPPER(v_prom_imp);
v_prom_imp_p1:=INSTR(v_prom_imp,'NON');
v_prom_imp_p2:=INSTR(v_prom_imp,'THREAT');
--INSERT INTO TEMP_PROM(PROM_NUM) VALUES(v_prom_imp_p1);
--INSERT INTO TEMP_PROM(PROM_NUM) VALUES(v_prom_imp_p2);
--COMMIT;
IF v_prom_imp_p1>0 /*OR v_prom_imp_p2>0*/ THEN
IF :NEW.prom_impact IS NOT NULL THEN
BEGIN
INSERT INTO TEMP_PROM(PROM_NUM,PROM_PROM_NUMBER,prom_impact,PROM_RELATION) VALUES(:NEW.prom_number,:NEW.prom_prom_number,:NEW.prom_impact,:NEW.prom_relation);
COMMIT;
EXCEPTION WHEN OTHERS THEN
INSERT INTO TEMP_PROM(prom_impact,PROM_RELATION) VALUES('failed1'||ecode,emesg);
END;
BEGIN
COMMIT;
UPDATE PROBLEMS SET PROM_IMPACT=:NEW.PROM_IMPACT WHERE prom_number=:NEW.prom_prom_number;
COMMIT;
EXCEPTION WHEN OTHERS THEN
ecode := SQLCODE;
emesg := SQLERRM;
-- BEGIN
INSERT INTO TEMP_PROM(prom_impact,PROM_RELATION) VALUES('failed'||ecode,emesg);
COMMIT;
-- EXCEPTION WHEN OTHERS THEN*/
NULL;
--END;
END;
END IF;
/*BEGIN
INSERT INTO TEMP_PROM(PROM_NUM,PROM_IMPACT) VALUES(:NEW.prom_prom_number,:NEW.prom_impact);
COMMIT;
EXCEPTION WHEN OTHERS THEN
INSERT INTO TEMP_PROM(prom_impact) VALUES('failed1');
NULL;
END;
--NULL;
-- END;
--COMMIT;
--END;
/* BEGIN
INSERT INTO TEMP_PROM(PROM_NUM,PROM_IMPACT) VALUES(:NEW.prom_prom_number,:NEW.prom_impact);
EXCEPTION WHEN OTHERS THEN
--INSERT INTO TEMP_PROM(prom_impact) VALUES('failed');
NULL;
END;
COMMIT;
--COMMIT;
/* ELSIF v_prom_imp_p2>0 THEN
-- v_prom_imp_p2
IF :NEW.prom_impact IS NOT NULL THEN
BEGIN
UPDATE PROBLEMS SET PROM_IMPACT=:NEW.PROM_IMPACT WHERE prom_number=:NEW.prom_prom_number;
EXCEPTION WHEN OTHERS THEN
BEGIN
ecode := SQLCODE;
emesg := SQLERRM;
--BEGIN
INSERT INTO TEMP_PROM(prom_impact,PROM_RELATION) VALUES('failed'||ecode,emesg);
COMMIT;
--EXCEPTION WHEN OTHERS THEN
NULL;
END;
NULL;
END;
COMMIT;
BEGIN
INSERT INTO TEMP_PROM(PROM_NUM,PROM_IMPACT) VALUES(:NEW.prom_prom_number,:NEW.prom_impact);
EXCEPTION WHEN OTHERS THEN
NULL;
END;
COMMIT;*/
--UPDATE PROBLEMS SET PROM_IMPACT=:NEW.PROM_IMPACT WHERE prom_number=:NEW.prom_prom_number;
-- COMMIT;
-- END IF;
END IF;
END IF;
EXCEPTION WHEN OTHERS THEN
INSERT INTO TEMP_PROM(prom_impact,PROM_RELATION) VALUES('failed2'||ecode,emesg);
COMMIT;
NULL;
END; -
Error in Trigger (PL/SQL: SQL Statement Ignored)
Here is the trigger:
CREATE OR REPLACE TRIGGER DRUGREPLACEMENT
AFTER INSERT ON PHARMACEUTICALS
REFERENCING NEW AS newDrugs
FOR EACH ROW
WHEN (newDrugs.drugname = newDrugs.genericname)
BEGIN
UPDATE prescription
SET pharmaceuticalid = newDrugs.drugID
WHERE pharmaceuticalid IN(SELECT pharmaceuticalid FROM prescription, pharmaceuticals WHERE drugid = pharmaceuticalid AND newDrugs.genericname = pharmaceutical.genericname);
END;
Error is:
Error(2,1): PL/SQL: SQL Statement ignored
Error(3,24): PL/SQL: ORA-00904: "NEWDRUGS"."DRUGID": invalid identifier
pharmaceuticalid is a number in my prescription table.
drugid is a number; genericname and drugname are both varchars in my pharmaceuticals table. Any idea why I am getting these errors? (I tried putting quotes around newdrugs.drugid but then it just said "newdrugs.drugid" is invalid. Thanks for your help
-BrianOne more thing. Your trigger is selecting from triggering table. So single row inserts will work, but multiple row inserts into PHARMACEUTICALS will fail with famous "table is mutating" error. There is no need to select from PHARMACEUTICALS. Change:
UPDATE ************
SET pharmaceuticalid = newDrugs.drugID
WHERE pharmaceuticalid IN(SELECT pharmaceuticalid FROM ************, pharmaceuticals WHERE drugid = pharmaceuticalid AND newDrugs.genericname = pharmaceutical.genericname);to
UPDATE ************
SET pharmaceuticalid = newDrugs.drugID
WHERE pharmaceuticalid IN (
SELECT pharmaceuticalid
FROM ************
WHERE drugid = :newDrugs.pharmaceuticalid
AND :newDrugs.genericname = pharmaceutical.genericname
);SY. -
How to handle the mutating error
create or replace trigger trg_t1
after insert on t1
begin
insert into t1 values(1);
end;
i got some error..1011927 wrote:
same table with the same trigger
Within trigger code do not issue SQL against same table upon which trigger is based; then no mutating error is thrown -
A SMALL EXAMPLE CODE FOR MUTATING ERROR
create or replace trigger TRG_T1
before insert on T1
begin
insert into T1 values(2);
end;
IF I TRY TO INSERT INTO T1 TABLE I GET A MUTATING ERROR HOW CAN I SOLVE??????joealexander wrote:
create or replace trigger TRG_T1
before insert on T1
begin
insert into T1 values(2);
end;
IF I TRY TO INSERT INTO T1 TABLE I GET A MUTATING ERROR HOW CAN I SOLVE??????
No, you do NOT get a mutating table error. You get this:
orcl112> create table t1(c1 number);
Table created.
Elapsed: 00:00:00.03
orcl112> create or replace trigger TRG_T1
2
3 before insert on T1
4
5 begin
6
7 insert into T1 values(2);
8
9 end;
10
11 /
Trigger created.
Elapsed: 00:00:00.04
orcl112> insert into t1 values (1);
insert into t1 values (1)
ERROR at line 1:
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "SCOTT.TRG_T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_T1'
ORA-06512: at "SCOTT.TRG_T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_T1'
ORA-06512: at "SCOTT.TRG_T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_T1'
ORA-06512: at "SCOTT.TRG_T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_T1'
ORA-06512: at "SCOTT.TRG_T1", line 3
ORA-04088: error during execution of trigger 'SCOTT.TRG_T1'
You are the second person today who has claimed to have seen this error and posted code that did not raise it. What is going on? -
How to over come mutating error
Can Anyone explain me how to OVERCOME Mutating error when firing a trigger ?
Thanks in advance.1. Don't use triggers
2. Don't perform DML on the same table your trigger is based on.
Without information (such as your table create statement and your trigger code) there's not a lot else we can say right now.
If you're getting a mutating table error, it's an indication that either your logic or your design (or both!) need rethinking... -
hi group
prem here
i am stillnot able to get the solution for mutating error.
let me give u scenario
before insert trigger -----on emp and in the trigger i select using the :new values , i get the mutating error,fine so far so good.
when it comes to solution i am unable tomake out from the link which was given in one of the replies,
can anyone pls in a simple words try to give a solution for:::::
scenario before i insert into emp a record i need to check if there is no record present in the table
solution ----
after going thru few sites i could zero in to a simple solution
1.creat a package variable store the :new variable
2.create a procedure or function to test if record exists and pass yesy or no to the trigger and then take the decision
also under what other cases do we get mutating errors
pls look into this problem and give me a solution
i hope it wll be of gr8 help to thers also
regards
premhttp://asktom.oracle.com/pls/ask/f?p=4950:8:14402737568889680009::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:469621337269
Rgds. -
Mutating error in different situations....
Hi All
I am getting mutating error in the following situtations.......please provide the suitable solution
1) Update same table on which trigger has been written
e.g. written trigger on abc table in which
command
update abc set col_nm=’New Name’
where col_id=
2) In trigger block called some procedure.
In procedure again update same table.
e.g. written trigger on abc table
it calls proc1 procedure.
In proc1 procedure it again update abc table
3)Select same table in same trigger e.g.
Written trigger on abc table and in that again query for selecting data.
Select sum(col_1) from abc
4)In trigger block called some procedure.
In procedure again select same table.
e.g. written trigger on abc table
it calls proc1 procedure.
In proc1 procedure it again select sum(col_1) from abc
5) In trigger block called some procedure in procedure update
another table which has a trigger in this trigger there is update or select query
for same table
e.g written trigger on abc table it calls procedure proc1 in proc1 it is updating some other table
xyz which has a update trigger in this update trigger it has updating and selecting data from
table abc.http://asktom.oracle.com/tkyte/Mutate/index.html
-
Mutating error -oracle version 8i
I have a problem of mutating error in Oracle.If u have any solution kindly forward it to me.
I have a table e1 as shown below:
SQL>select * from e1;
code id transdate destination
The trigger is to fire after insert of a row
when "id" is 2.
The trigger calls a procedure which calculates a new
"transdate" and inserts a new record into the table e1 with
the newly calculated "transdate","id" as 12 and all the other
columns(code and destination)remains the same.
I have to pass "code" and "transdate" as input parameters to the
procedure.
Inside the procedure i have a select statement which fetches
the record from table e1(which is being inserted and fires a trigger).
This is the rootcause of the problem.
Since the trigger is firing after Insert,looping
happens and hence gives out the same error.
I even tried to use the PRAGMA AUTONOMOUS_TRANSACTION but it is not
supported in our version.
Please let me know if there is some other alternate to this.
--Regards,
Arthihttp://osi.oracle.com/~tkyte/Mutate/index.html
-
Master/Details tables with CASCADE delete giving "table mutating" error
Hi,
I have two tables in a master/details configuration (table 1 is "master", table 2 is "details").
The details has a FK contraint relation to the master with a CASCADE delete (so when the master is deleted, all associated details are deleted).
The master table has a column "last_updated_datetime" which is updated by a master table update trigger whenever most (excluding the last_updated_datetime column) columns are updated.
The details table has an update trigger which also updates the master table's last_updated_datetime column (whenever a detail row is changed).
The details table also has a delete trigger which also updated the master table's last_updated_datetime column (whenever a detail row is deleted).
The problem I have is: When the master record is deleted, which cascade deletes the details record(s), the delete trigger on the details table throws a "table is mutating" error.
I understand that the "mutating" error is "correct" because the master record is being deleted.
But is there some way I can get around this problem (for example, having the details table delete trigger not update the master table last_updated_datetime) when it's this cascade delete?
Thanks for your help!create table master (
id number primary key,
changed date not null
create table detail (
id number primary key,
master_id references master (id) on delete cascade
create or replace package pkg is
master_deleted boolean := false;
end;
create or replace trigger bds_master
before delete on master
begin
pkg.master_deleted := true;
end;
create or replace trigger ads_master
after delete on master
begin
pkg.master_deleted := false;
end;
create or replace trigger adr_detail
after delete on detail
for each row
begin
if not pkg.master_deleted then
update master
set changed = sysdate
where id = :old.master_id;
end if;
end;
insert into master values (1, sysdate - 10);
insert into detail values (11, 1);
insert into detail values (12, 1);
insert into master values (2, sysdate - 10);
insert into detail values (21, 2);
insert into detail values (22, 2);
insert into master values (3, sysdate - 10);
insert into detail values (31, 3);
insert into detail values (32, 3);
commit;
select * from master order by id;
ID CHANGED
1 01.09.08
2 01.09.08
3 01.09.08
select * from detail order by id;
ID MASTER_ID
11 1
12 1
21 2
22 2
31 3
32 3
delete detail where id = 21;
select * from master order by id;
ID CHANGED
1 01.09.08
2 11.09.08
3 01.09.08
select * from detail order by id;
ID MASTER_ID
11 1
12 1
22 2
31 3
32 3
delete master where id in (1, 2);
select * from master order by id;
ID CHANGED
3 01.09.08
select * from detail order by id;
ID MASTER_ID
31 3
32 3 Regards,
Zlatko
Edited by: Zlatko Sirotic on Sep 11, 2008 11:15 PM
This is disaster - ten attempts to format code! -
What is meant by Mutating error
Hi friends,
i am getting mutating error,
how to overcome this problem.
thanking u.
hemaTo over come its better follow
This package will contain yours rowid which is being insert of emp table
create or replace package state_pkg
as
type ridArray is table of rowid index by binary_integer;
newRows ridArray;
empty ridArray;
end;
This will flush or empty yours array wid empty one array cause there may be rowids
create or replace trigger emp_bi
before insert on emp
begin
state_pkg.newRows:=state_pkg.empty;
end;
This will move insert rows rowid in newRows array
create or replace trigger emp_ai
after insert on emp for each row
begin
state_pkg.newRows(state_pkg.newRows.count+1):=:new.rowid;
end;
This will backup yours data in empback which is being inserting in emp table note here at line 2 there is no for each row clause which means its statment level trigger and statment level trigger dont mutate because its fire at table level not for individaul rows.
create or replace trigger emp_aif
--> after insert on emp
begin
for i in 1..state_pkg.newRows.count
loop
insert into empback
select * from emp where rowid=state_pkg.newRows(i);
end loop;
end;
what this code does its just capture the rowids in a temp (newRows) array and then after getting insert records in table each row is fetching from emp table into empback on behalf of package array newRows (which contains rowid)
I hope u got it how to avoid mutate.
Khurram Siddiqui
[email protected]
Maybe you are looking for
-
Report Viewer Configuration Error
Hi, I manage a TFS 2010 Server that makes heavy use of Reporting Services, and I have a user that reported the following error when attempting to open a report in the Microsoft Visual Studio Report Viewer: Report Viewer Configuration Error The Report
-
My Ipod Touch is not very well!
Hello, When I plug my Ipod in to my computer you can hear the beep that says the computer has seen the connection, yet it cannot be seen in My Computer. When the Itunes window comes up, there is a box with this message in it: "Itunes could not connec
-
Best settings for export to iphone?
Hello, I wonder if anyone has found the best way of doing this, I couldn't find any recommendations on the apple site. I'm editing HDV PAL, should I resize in FCP before export or let compressor do all the resizing? Should I deinterlace? Square pixel
-
Querying table for content in ST22.
Dear all, I have a requirement to gather certain abap short dump e.g. like only Error Id is TIME_OUT into an xls sheet. System throws out nearly 100+ TIME_OUT error each day and we want to put that into an xls sheet. So I?m thinking to write an abap
-
Notes Deleted on Mac and iCloud
I think my Notes have been deleted from the Mac and iCloud by accident. Is there a way to find the deleted Notes? There is nothing on my computer and iCloud does not appear to have any previous data. Does iCloud retain a prior day's data for recov