I have a procedure P1.
TABLES T1,T2 and TRIGGER TRG1
Below is the Trigger Event:
CREATE OR REPLACE TRIGGER
AFTER UPDATE OF RECORD_STATUS
ON T1 WHEN (NEW.RECORD_STATUS='U')
BEGIN
.
--Calling Procedure
P1();
.
END;
I am calling a procedure with parameters. Inside procedure P1 using those parameters I am selecting the data from T1 and doing some validations and inserting into T2.
After Successful insert I am updating the RECORD_STATUS='I' in the the procedure.
Now I performed below statement:
UPDATE T1 SET RECORD_STATUS='U'
Since the trigger event occurs here it is calling my procedure and inside the procedure since I am updating the same column again trigger is firing and leads to dead lock. Please help me in this.
Thanks in advance.
First of all some advice, NEVER use triggers unless your desperate.
If your already updating the table in a stored procedure, add the logic into your P1 procedure, or create a second procedure with the additional functionality and call it from P1.
Good citizenship is part of database development. All transactions on those tables should follow the same order. Using a trigger to update the same table is not good citizenship.
If you really have to do something like this, one thing you can try, is to use an Autonomous Transaction
Related
There is a procedure which have insert operation in middle for a table. The table contain a trigger and because of that the entire transaction getting hold. Is there are anyway to make trigger run on separate session and after insert operation procedure runs without waiting for the trigger to complete.
Both procedure and the trigger are
PRAGMA AUTONOMOUS_TRANSACTION
You could try runnig the trigger part as dbms_job ... as follows:
CREATE OR REPLACE TRIGGER myrigger
AFTER INSERT
ON mytable
REFERENCING NEW AS New OLD AS Old
FOR EACH ROW
DECLARE
l_job number;
begin
dbms_job.submit( l_job, 'MYPACKAGE.MYFUNCTION(''' || :new.myField || ''');' );
END ;
/
If Trigger is based on insert operation, it will hold the current session, until that trigger action is completed. This is required to maintain integrity at database side. It may be possible to design a procedure & trigger in a better way if the requirements are known.
Is it possible to fire a trigger before a specific procedure starts execution?
For example I have a package "A", and a procedure "B" inside package "A".
So when I call A.B procedure i want to fire a trigger.
I'm using oracle 11gR2.
is it possible to fire a trigger before a specific procedure starts execution?
No, you cannot manually fire a trigger without any DML operation on the table. Triggers are designed to act implicitly on any DML action. Trigger is a (side)effect of an action and not an action in itself.
I don't think you need a trigger for your requirement. You could call a procedure before executing the procedure A.B. Put your business logic accordingly. PL/SQL is a procedural language. So, if you put another procedure say procedure C before A.B, then A.C will be executed before A.B.
Triggers cannot be called directly. Instead they are fired automatically when you perform an insert/update or delete on a table that has triggers. So like in your case, on calling A.B procedure, you can perform an insert / update or delete that will force a trigger to fire.
If you have an existing trigger on a table and you want its logic to be executed under other circumstances, then place the logic in a procedure and call that procedure from the trigger, and from any other code that needs to execute it.
I have a table A into which I am inserting data. Then some calculation is being done updating the same table A.
I want to fire a trigger, which calls a Procedure A after the completion of data insertion ( after insert and update ).
How do I do this?
Is there any other way to do it automatically... Or do I have to run Procedure A manualy after the completion of data insertion in table A.
More simply, I would like to know how to fire a trigger after inserting a few rows and a commit, i.e. not for each row.
You can define your trigger to be fired for each row or for each statement (FOR EACH ROW option).
If I understood you right, you would like to fire the trigger after a bunch of statements? Don't think you can. Even if you can, I would rather not do it. They scatter your program flow / logic and make it harder to understand later how your software works.
Regards
If I understand your question correctly, you want the trigger to fire after you completed your transaction consisting of several insert/update statements? If that is the case, I think you should consider calling your Procedure A in your program flow right after the insert/update operations are done.
In other words: A trigger would only be useful, if it should be called for each row or for each statement.
Add one column to your table: e.g "FINAL_ACTION". Leave this column untouched untill your anticipated final action. Then have your trigger get fired only with this clause:
REFERENCING NEW AS NEWREC OLD AS OLDREC
FOR EACH ROW
WHEN (NEWREC.FINAL_ACTION <> OLDREC.FINAL_ACTION)
DECLARE
--YOUR DECLARATIONS
BEGIN
--DO SOMETHING
END;
I have a very basic doubt.
Suppose we heve two after insert trigger TR1 and TR2 on table 1.
One trigger updates same Table 1 and second trigger Call procedure ..
I need TR1 to execute before TR2..
How this can be achieved..
Thanks
In Oracle 11G you can use the PRECEDES or FOLLOWS clauses e.g.
create trigger TR2
after insert on table1
for each row
follows TR1 -------------------------------------------------<<
begin
...
end;
Prior to 11G the order if firing was indeterminate; the only way to ensure correct execution was to combine the 2 triggers into 1.
I want to fire a trigger whenever an insert command is fired..
The trigger will access a pl/sql file which can change anytime..
So the query is, if we design the trigger, how can we make sure this dynamic thing happens.. As during the stored procedure, it is not working..
I think - it should work for
1) External Procedures
2) Execute Statement
Please correct me, if I am wrong.. I was working on External Procedures but i am not able to find the way to execute the external procedure from here on..
SQL> CREATE OR REPLACE FUNCTION Plstojavafac_func (N NUMBER) RETURN NUMBER AS
2 LANGUAGE JAVA
3 NAME 'Factorial.J_calcFactorial(int) return int';
4 /
##########################################
SQL> CREATE OR REPLACE TRIGGER student_after_insert
2 AFTER INSERT
3 ON student
4 FOR EACH ROW
How to call the procedure from heree... And does my interpretations are right,, plz suggest..
Thanks.
either use procedure for inserting where you will handle your data and then quit procedure (cancel insert) or make successful insert, or do your data handling in trigger not in procedure