Solved

Oracle custom trigger

Posted on 2014-10-31
7
369 Views
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.
0
Comment
Question by:tancat
  • 4
  • 3
7 Comments
 
LVL 34

Accepted Solution

by:
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.
0
 

Author Comment

by:tancat
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.
0
 
LVL 34

Expert Comment

by:johnsone
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.
0
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

by:tancat
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...
0
 
LVL 34

Expert Comment

by:johnsone
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.
0
 

Author Comment

by:tancat
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!
0
 
LVL 34

Expert Comment

by:johnsone
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.
0

Featured Post

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.

Join & Write a Comment

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html 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…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

747 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

10 Experts available now in Live!

Get 1:1 Help Now