Oracle custom trigger

Posted on 2014-10-31
Last Modified: 2014-11-10
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.
Question by:tancat
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 35

Accepted Solution

johnsone earned 500 total points
ID: 40416209
Why put the logic for a trigger into a procedure?  Why not put all the logic in the trigger?  Saves a context switch from the trigger to the procedure and after all a trigger is just a specialized procedure anyway.  Within the trigger, you would have access to all the columns that you needed.

Author Comment

ID: 40416249
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.
LVL 35

Expert Comment

ID: 40416338
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.
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

ID: 40418766
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...
LVL 35

Expert Comment

ID: 40419184
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.

Author Comment

ID: 40432812
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!
LVL 35

Expert Comment

ID: 40432818
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.

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to trim oracle sql sentence in unix 17 86
sum of columns in a row in oracle 3 60
Migration from SQL server to oracle (XML input) 4 68
replicate in oracle 13 43
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question