ORACLE trigger before procedure execution - oracle

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.

Related

Oracle deadlock trigger

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

PL/SQL - will statements execute only once a procedure is completed?

Hopefully this is a simple question. Say I have a block like this:
BEGIN
a_random_procedure ('input','output');
... DML statements ...
END;
Question: Will DML statements execute only after a_random_procedure is completed? For that matter, will anything after a_random_procedure execute only after it is completed?
ADD-ON: What about when a_random_procedure is executing another procedure within it?
Yes, the "DML statements" will execute only after "a_random_procedure" is completed. If you have another procedure within it, the first procedure will wait until the procedure within it is completed too. In any moment when you call another procedure or function, the next code line will wait until the procedure or function called before, has finished his execution
PL/SQL is like in name of launguage - procedural, so the answer for Your question is yes, dml will be executed after procedure finish, even if procedure calls other procs/functions. Yo can take care about case when Your procedure crash, then it will not be completed, and then thera are 2 options, if the exception in procedure is handled then Your dml will be executed. If exception is not handled, it will be populated level up, in this case to Your anonymous block, and crash(stop) it as well and it will not execute DML.

How to fire a trigger after finishing data insert in a table

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;

Does on-select trigger fire during/after query-procedure trigger fires?

I have a form that I'm looking at, which makes use of stored procedures as data block source. I'm trying to find when/where the block gets populated.
The block as query data source type as Procedure, but the procedure listed as the data source name does nothing - literally nothing. The code in the procedure is
begin
null;
end;
Going through the list of procedures in the package responsible for handling the form, I found a procedure which does populates the data.
Searching for this procedure, I found that this procedure is invoked during ON-SELECT trigger.
So - does ON-SELECT trigger get fired during/after QUERY-PROCEDURE event? If not when does it get fired?
I'm using Oracle Forms10.1.2.3.0, 32-bit on Windows Vista.
From the Forms on-line help for ON_SELECT:
Fires when Oracle Forms would normally
execute the open cursor, parse, and
execute phases of a query, to identify
the records in the database that match
the current query criteria.
Use an On-Select trigger to open and
execute the database cursor.
Specifically, use this trigger when
you are retrieving data from a
non-ORACLE data source. The On-Select
trigger can be used in conjunction
with the On-Fetch trigger to replace
the processing that normally occurs in
the EXECUTE_QUERY built-in subprogram.

How to use External Procedures in Triggers on Oracle 11g

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

Resources