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
  • 4
  • 3
LVL 34

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 34

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.
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


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 34

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 34

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Single ERP VS muttiple Application or Systems 6 61
File generation using utl_file 4 45
selective queries 7 20
Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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 shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now