I have a procedure that is supposed to take an employee from a certain table and insert it into another table that records who is in a leadership role for a certain period.
The thing is, there can only be a maximum of two employees in leadership roles for a given period.
The primary key in the table is the pair (employee,month) and there is no manual insert, so it's all done trough the procedure.
How do I structure it so that concurrent accesses can't register more than 2 employees in a leadership role for the same month?
I thought about getting a COUNT(*) on the number of rows for that month, but if an insert occurs after the count is registered, it would no longer be valid. Is there any way to control this right on the procedure or do I need to resort a trigger to check after each insert and rollback if the number exceeds 2?
I cannot change the structure of the table, just the procedure.
ORA-04091
error. Could work with a before insert trigger. Another way: useDBMS_LOCK
to serialize the inserts. You can do this in your procedure, but this protects inserts coming from this procedure only. Another way: aggregate materialized view with a check constraint on theCOUNT
column, an overkill for this task. I would just go with the before insert trigger I guess. But think about updates too.