2

I have a table trigger like below:

CREATE OR REPLACE TRIGGER PAT_BUR_DOB_TRG
 BEFORE UPDATE OF DOB
 ON PAT
 REFERENCING OLD AS OLD NEW AS NEW
 FOR EACH ROW
-- PL/SQL Block
begin
  tgln_sys_error_pkg.compare_dates(trunc(add_months(:new.dob, -12)),
                                   trunc(tgln_sys_error_pkg.GET_LIST_DATE(:old.pat_id)),
                                   tgln_sys_errnums_pkg.en_retr_waitlist_date);
end;

-------------------------------------- 

I have a package which is called by the trigger above, the code for the package is like below:

CREATE OR REPLACE PACKAGE TGLN_SYS_ERROR_PKG AS

/* To compare two dates against each other. */
PROCEDURE COMPARE_DATES
(P_DATE_LOW date
,P_DATE_HIGH date
,P_ERROR_CODE number
);

FUNCTION GET_LIST_DATE
 (P_PAT_ID number)
 RETURN DATE;

END TGLN_SYS_ERROR_PKG;
--------------------------------------   

The package body is like below:

CREATE OR REPLACE PACKAGE BODY TGLN_SYS_ERROR_PKG AS

FUNCTION GET_LIST_DATE(P_PAT_ID number) RETURN DATE IS
  v_ret_date date;
begin

  --select to_date('01-JAN-1980') into p_Date from dual; 

  select max(pwl.eff_date)
    into v_ret_date
    from pat, pat_register pr, pat_register_org_det prod, pat_wait_list pwl
   where pat.pat_id = pr.pat_id
     and pr.patr_id = prod.patr_id
     and prod.prod_id = pwl.prod_id
     and pat.pat_id = P_PAT_ID
     and rownum < 2
     AND pwl.exp_date is null;
  return nvl(v_ret_date, to_date(null));
exception
  when no_data_found then
    return to_date(null);
end GET_LIST_DATE;


PROCEDURE COMPARE_DATES
(P_DATE_LOW date
,P_DATE_HIGH date
,P_ERROR_CODE number
)
IS
begin
if nvl(p_date_low,sysdate-10000)>nvl(p_date_high,sysdate+10000) then
raise_application_error(p_error_code,null);
end if;
end compare_dates;

end TGLN_SYS_ERROR_PKG;
--------------------------------------    
CREATE OR REPLACE PACKAGE TGLN_SYS_ERRNUMS_PKG IS
en_retr_waitlist_date CONSTANT INTEGER := -20088; --Patient waitlist effective dates must not be less than or equal to patient's date of birth minus one year ( DOB - 1 year).     
END TGLN_SYS_ERRNUMS_PKG;
-------------------------------------- 

Each time when Oracle Forms update DOB data, I get error like below:

Oracle Form FRM-40735: ON-ERROR trigger raised unhandled exception ORA-06502

But, when I hard code like below:

select to_date('01-JAN-1980') into p_Date from dual; 

to instead of this paragraph code like below, form works fine.

  select max(pwl.eff_date)
    into v_ret_date
    from pat, pat_register pr, pat_register_org_det prod, pat_wait_list pwl
   where pat.pat_id = pr.pat_id
     and pr.patr_id = prod.patr_id
     and prod.prod_id = pwl.prod_id
     and pat.pat_id = P_PAT_ID
     and rownum < 2
     AND pwl.exp_date is null;

I did replace p_pat_id to a real value, it pops up a trigger error

ORA-04091: table TGLN.PAT is mutating, trigger/function may not see it 
ORA-06512: at "TGLN.TGLN_SYS_ERROR_PKG",
line 130 ORA-06512: at "TGLN.PAT_BUR_DOB_TRG",
line 26 ORA-04088: error during execution of trigger 'TGLN.PAT_BUR_DOB_TRG'
View program sources of error stack?" 

So, how to fix the bug? I can not hard code the date values

Sathyajith Bhat
  • 21,321
  • 22
  • 95
  • 134
user1308891
  • 23
  • 1
  • 1
  • 4
  • 2
    Didn't you just ask this a few minutes/hours ago? Could you at least have conserved the formatting fixes? Don't delete and repost questions, that is annoying (and eventually will lead to an automatic question ban if you do it too often.) – Mat Apr 02 '12 at 19:57
  • @user1308891:`tgln_sys_error_pkg.compare_dates` is not in spec ,its a private procedure .How can you call this procedure from trigger.Please correct your code – Gaurav Soni Apr 02 '12 at 20:00
  • Sorry, I am new on Stackoverflow, not good on how to use it. Please! – user1308891 Apr 02 '12 at 20:01
  • I add tgln_sys_error_pkg.compare_dates spec into the post. – user1308891 Apr 02 '12 at 20:09
  • Don't know why, if I comment out code "select max(pwl.eff_date)...", trigger is working, but I really need this select statement. – user1308891 Apr 02 '12 at 20:14
  • I did replace p_pat_id to a real value, it popup a trigger error:"ORA-04091: table TGLN.PAT is mutating, trigger/function may not see it ORA-06512: at "TGLN.TGLN_SYS_ERROR_PKG", line 130 ORA-06512: at "TGLN.PAT_BUR_DOB_TRG", line 26 ORA-04088: error during execution of trigger 'TGLN.PAT_BUR_DOB_TRG' View program sources of error stack?" – user1308891 Apr 02 '12 at 20:18
  • if remove "into v_ret_date" get a compile error. "an into clause is expect in this select statement" – user1308891 Apr 02 '12 at 20:21
  • copy this select max(pwl.eff_date)... query from the function and try to execute this in another editor, get correct date, without any problem. – user1308891 Apr 02 '12 at 20:22
  • When you create a trigger against a table, you can't modify/query that table until the trigger/function has completed. – Gaurav Soni Apr 02 '12 at 20:29
  • Recompile the trigger & package & all incompiled objects. and run the update statement : update pat set pat.dob = to_date('01-JAN-1985') where pat.pat_id = 21592; get the same error like before error is : ORA-04091: table TGLN.PAT is mutating, trigger/function may not see it ORA-06512: at "TGLN.TGLN_SYS_ERROR_PKG", line 130 ORA-06512: at "TGLN.PAT_BUR_DOB_TRG", line 26 ORA-04088: error during execution of trigger 'TGLN.PAT_BUR_DOB_TRG' View program sources of error stack? – user1308891 Apr 02 '12 at 20:33

1 Answers1

1

Error:

ORA-04091: table name is mutating, trigger/function may not see it/

Your Error

trigger error:"ORA-04091: table TGLN.PAT is mutating

Cause:

A statement executed a trigger or custom PL/SQL function. That trigger/function tried to modify or query a table that is currently being modified by the statement that fired the trigger/function.

Your Cause

 --you're not supposed to query a table that is currently modified 
 --In you're case its `PAT` table being updated and querying at same time
 select max(pwl.eff_date)
 into v_ret_date
 from pat,  --here you are querying your PAT table,while updating the same table
      pat_register pr, pat_register_org_det prod, pat_wait_list pwl
  where pat.pat_id = pr.pat_id
   and pr.patr_id = prod.patr_id
   and prod.prod_id = pwl.prod_id
   and pat.pat_id = P_PAT_ID
   and rownum < 2
   AND pwl.exp_date is null;

Action:

The options to resolve this Oracle error are: Re-write the trigger/function so that it does not try to modify/query the table PAT in question.

Reference

Community
  • 1
  • 1
Gaurav Soni
  • 6,278
  • 9
  • 52
  • 72
  • Thanks Gaurav!!! I will modify the select statement tomorrow morning when I am in office, and then will let you know the result. – user1308891 Apr 03 '12 at 02:24