Link to home
Start Free TrialLog in
Avatar of tancat
tancatFlag for United States of America

asked on

Oracle custom trigger

I am using Oracle EBS 12.1, db 10g, and looking for the correct way to accomplish a custom task.

The employee's employment category (Salaried, Fulltime, Partttime, etc.) is kept in the assignment table.  The users can either change the employment category as a 'Correction' (the wrong employment category was originally entered) or as an 'Update' (the employee received a promotion or demotion).  The only way to tell the difference from the database is to see if there is a new set of effective dates.  

Incidentally, whether the user intended a 'Correction' or an 'Update', the employment category is always implemented in an update statement in the database: if the user intended an 'Update', a new assignment row is created with a new set of effective dates and then that row is updated with the employment category; if the user intended a 'Correction', then the existing row is updated with the employment category.  

I put a trigger on the assignment table after an update of the employment category, which calls a procedure that inserts a row into a custom table.  However, prior to inserting the row into the custom table, I want to check the effective dates in the assignment table.  However, I can't do this because I cannot query the assignment table until the transaction is completed.  

I understand that I could use PRAGMA AUTONOMOUS_TRANSACTION to get around this, but I have heard this is not necessarily always a good solution.  

After the row is inserted into the custom table, it sits there until a user runs a concurrent program that does something with the data in the table.  I suppose I could prepend to this concurrent process a check on the data to eliminate the rows that I really don't want.  

What is the correct way/best practice to implement this custom task?  Some background info, or reasons why, would be much appreciated with the answer.
ASKER CERTIFIED SOLUTION
Avatar of johnsone
johnsone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tancat

ASKER

There are actually two triggers, one on the person table for inserts/updates/deletes, and one on the assignment table for updates.  They both call the same procedure, which contains logic to figure out whether we really want to insert a new row into the custom table, or if we want to update an existing row, or mark an existing row in the custom table as processed (in the case of a delete on the person table).  

Since the problem is just with the update to the assignment table, I could put just the logic to determine 'Correction' or 'Update' in the trigger, and then call the procedure if its an 'Update'.  

In the past I have been told by more senior developers that it is not a good practice to put very much logic in a trigger, that triggers should almost always call a procedure to do the work.  But I don't know the reasoning behind that decision.  Possibly it is related to mutating tables.
We have had triggers with thousands of lines of code in them.  Never had a problem.

If tables would be mutating because of trigger, then you have a design flaw or people that don't know the way to design triggers.

Any logic that requires the use of the fields in the table should be in the trigger.
Avatar of tancat

ASKER

I moved the logic to determine 'Correction' or 'Update' into the trigger on that table, and only call the procedure if it is a 'Correction'.  Everything seems to be working correctly (to be confirmed by my BA), and I asked my DBA to review the code and let me know if he sees any problems.  

I can't speak to the design of the database that I worked on at my last company; it has been around since 1991 and there were numerous problems with it, mostly maintenance issues due to multiple programmers, database versions, best practices, etc. over the years.  It has morphed and grown so much that there is probably nothing left of the original design.  

Does the code in your triggers actually "do" anything (inserts/updates/deletes to other tables)?  Or do they just figure things out and make decisions?  Although, I wouldn't think it would matter whether the code that "does" anything is in a trigger or a procedure that the trigger calls...
The triggers do everything, insert/update/delete/select.  All is carefully designed.  It doesn't do anything that is doesn't have to and is tuned as far as it can be.  After all, trigger code executes without the user knowing it and they shouldn't be able to see the time it takes to run it.

There are a couple of procedure calls within the code as well, but those are for code that is common to many parts of the application and aren't specific to the trigger.
Avatar of tancat

ASKER

Sorry for the late response; I still don't entirely understand what causes a mutating trigger, but I found a couple of examples that I can play around with in our sandbox.  Thanks for your help, johnsone!
At the most basic level a mutating table is caused by a row level trigger that selects from the table that the trigger is fired from.  That is what causes it in almost every case that I have seen.