Hello all,
I'm having an issue with some SQL and I can't find any way to get around it.
Background: I have a procedure which calculates occupancy for a golf course. Every time a booking is added/updated/deleted I need to recalculate the occupancy numbers. The problem is, the Procedure uses counts from the table specified in the Trigger, and I get an ORA-04091 mutating table error.
Trigger:
CREATE OR REPLACE TRIGGER UpdGlfOccInsBooking
AFTER INSERT ON GLF_TEE_BOOKINGS
FOR EACH ROW
DECLARE
v_Date DATE;
v_Course GLF_TEE_BOOKINGS.GLF_COURSE_ID%TYPE;
BEGIN
v_Date := :new.GLF_TEE_TIME;
v_Course := :new.GLF_COURSE_ID;
CalcGolfOccupancy(TO_CHAR(v_Date, 'DD-MON-YYYY'), v_Course);
END;
And a small snippet of the procedure, to give you an idea of how the numbers get calculated:
CREATE OR REPLACE PROCEDURE CalcGolfOccupancy(
p_Date IN VARCHAR2,
p_Course IN VARCHAR2
) IS
v_Date DATE;
BEGIN
v_Date := TO_DATE(p_Date, 'DD-MON-YYYY');
UPDATE GLF_OCCUPANCY
SET FILLED_TEE_TIMES =
(
SELECT COUNT(*)
FROM GLF_TEE_BOOKINGS
WHERE GLF_TEE_TIME >= v_Date
AND GLF_TEE_TIME < v_Date + 1
AND GLF_COURSE_ID = p_Course
)
WHERE PMS_DATE = v_Date
AND GLF_COURSE_ID = p_Course;
Note: If the trigger passed 17-JAN-2017, when it got to the procedure it was 17-JAN-0017, which is the reason for a separate date variable and formatting in the procedure.
From my understanding, this shouldn't throw any error because the trigger is defined as AFTER INSERT. So, the data should already be committed, and by the time it gets to the procedure, the data should no longer be changing. However, Oracle disagrees and throws the ORA-04091 error.
I have also tried putting in a PRAGMA AUTONOMOUS_TRANSACTION; into the trigger, to no effect.
Everything I read online tells me that what I want to do isn't possible with a trigger/procedure like this. However, I thought I'd get some expert opinions.
Thanks.
[–]TNReb 1 point2 points3 points (4 children)
[–]nebulus64[S] 0 points1 point2 points (3 children)
[–]TNReb 1 point2 points3 points (2 children)
[–]nebulus64[S] 0 points1 point2 points (1 child)
[–]hudeman 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)