Solved

Oracle custom trigger

Posted on 2014-10-31
7
407 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
[X]
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
7 Comments
 
LVL 35

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 35

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 35

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 35

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

707 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