Calling a Trigger from a Procedure. [message #156260] |
Tue, 24 January 2006 03:22 |
nirmalnarayan
Messages: 261 Registered: April 2005 Location: India
|
Senior Member |
|
|
This question was asked in a recent interview.
'Can we call a trigger inside a Procedure?'
I gave the answer that there is no need to call a trigger inside a proc. as the trig. get executed automatically on any of DML statements, but the interviewer was not satisfied. He again repeated the same question, so i said 'NO'
Here is the script
----------------------------------
SQL> create or replace trigger ftrig
2 after insert on emp
3 begin
4 dbms_output.put_line('Trigger ');
5 exception when others then
6 null;
7 end;
8 /
Trigger created.
SQL> create or replace procedure calproc is
2 begin
3 execute immediate 'call ftrig()';
4 exception when others then
5 null;
6 end;
7 /
Procedure created.
SQL> call calproc();
Call completed.
SQL> call ftrig();
call ftrig()
*
ERROR at line 1:
ORA-06576: not a valid function or procedure name
-----------------------------------------------
We can call trigger using EXECUTE IMMEDIATE, but still i don't understand the stupid question from the stupid interviewer, why should one need to call a trigger inside a procedure?
|
|
|
|
|
|
Re: Calling a Trigger from a Procedure. [message #156360 is a reply to message #156276] |
Tue, 24 January 2006 23:26 |
nirmalnarayan
Messages: 261 Registered: April 2005 Location: India
|
Senior Member |
|
|
Frank wrote on Tue, 24 January 2006 07:06 |
Quote: | 3 execute immediate 'call ftrig()';
|
This is not valid pl/sql anyway. Call is a sqlplus keyword.
|
Then why it did'nt give me any error ?
Anyway my question is not that, Why should one want to call a trigger inside a Procedure in PL/SQL ?
Rgds,
Nirmal Narayanan.
|
|
|
Re: Calling a Trigger from a Procedure. [message #156367 is a reply to message #156360] |
Wed, 25 January 2006 00:49 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
nirmalnarayan wrote on Wed, 25 January 2006 06:26 |
Then why it did'nt give me any error ?
|
Quote: | 4 exception when others then
5 null;
6 end;
|
Because you told it not to give an error.
As Maaher said, calling a trigger only makes in forms.
|
|
|
Re: Calling a Trigger from a Procedure. [message #156369 is a reply to message #156276] |
Wed, 25 January 2006 01:11 |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
Frank wrote on Tue, 24 January 2006 23:06 | Call is a sqlplus keyword.
|
Correction, EXEC is a SQLPLUS keyword. CALL is a SQL command. The giveaway is that you have to terminate a CALL with a semi-colon or slash to run it in SQL*Plus.
_____________
Ross Leishman
|
|
|
|
|
Re: Calling a Trigger from a Procedure. [message #156408 is a reply to message #156276] |
Wed, 25 January 2006 03:58 |
nirmalnarayan
Messages: 261 Registered: April 2005 Location: India
|
Senior Member |
|
|
Frank wrote on Tue, 24 January 2006 07:06 |
Quote: | 3 execute immediate 'call ftrig()';
|
This is not valid pl/sql anyway. Call is a sqlplus keyword.
|
I think you are wrong 'Call can be used inside Execute Immediate '
Here is the script
SQL> create or replace procedure calproc is
2 begin
3 dbms_output.put_line('INSIDE PROCEDURE CALPROC');
4 end;
5 /
Procedure created.
SQL> create or replace procedure callfromproc is
2 begin
3 execute immediate 'call calproc()';
4 end;
5 /
Procedure created.
SQL> call callfromproc();
INSIDE PROCEDURE CALPROC
Call completed.
|
|
|
Re: Calling a Trigger from a Procedure. [message #156418 is a reply to message #156404] |
Wed, 25 January 2006 04:18 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
nirmalnarayan wrote on Wed, 25 January 2006 10:33 | Hi,
Anyway my question is not that, Why should one want to call a trigger inside a Procedure in PL/SQL ?
| One can want so many things but somethings are just not possible, like calling a DML trigger manually without performing the triggering event (i.e. the DML).
nirmalnarayan wrote on Wed, 25 January 2006 10:33 | Then Why do they say "PL-SQL is in Sync with SQL" from Oracle 9i onwards?
| Because in elder versions of Oracle, some advanced SQL features weren't available in PL/SQL. I don't see the relevance to your question. DECODE can also only be used in SQL. For CALL, the same restrictions apply.
Now, I have tested it. Using EXECUTE IMMEDIATE, you can call a procedure or function, but not a trigger.
Using this script
SET SERVEROUT ON
CREATE PROCEDURE PRC$FOO
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Procedure foo called');
END;
/
CREATE TABLE mhe_foo(id NUMBER)
/
CREATE TRIGGER trg_mhe_foo
BEFORE INSERT ON mhe_foo
BEGIN
dbms_output.put_line('Trigger on mhe_foo called.');
END;
/
BEGIN
EXECUTE IMMEDIATE 'CALL prc$foo()'; -- procedure call
END;
/
BEGIN
EXECUTE IMMEDIATE 'CALL trg_mhe_foo()'; -- trigger call, will fail.
END;
/
DROP TABLE mhe_foo
/
DROP PROCEDURE PRC$FOO
/ I got this result:
SQL> select banner from v$version
2 /
BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.5.0 - 64bit Production
PL/SQL Release 9.2.0.5.0 - Production
CORE 9.2.0.6.0 Production
TNS for IBM/AIX RISC System/6000: Version 9.2.0.5.0 - Production
NLSRTL Version 9.2.0.5.0 - Production
SQL> @C:\useful\orafaq
Procedure created.
Table created.
Trigger created.
Procedure foo called
PL/SQL procedure successfully completed.
BEGIN
*
ERROR at line 1:
ORA-06576: not a valid function or procedure name
ORA-06512: at line 2
Table dropped.
Procedure dropped. The ORA message is clear: it has to be a valid function or procedure.
MHE
|
|
|
Re: Calling a Trigger from a Procedure. [message #156465 is a reply to message #156418] |
Wed, 25 January 2006 05:20 |
nirmalnarayan
Messages: 261 Registered: April 2005 Location: India
|
Senior Member |
|
|
I agree with you that Trigger cannot be called from a Procedure (IN PL/SQL, NOT IN FORMS), but Procedures and Functions can and 'CALL' can be used with EXECUTE IMMEDIATE inside a procedure.
This is what i understand and this is not some interviewers at MPHASIS,Bangalore does'nt understand.
Thanks and Regards
Nirmal N.
|
|
|
|
|
Re: Calling a Trigger from a Procedure. [message #332783 is a reply to message #332776] |
Wed, 09 July 2008 12:47 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
Quite simply incorrect. I'd like to see them turn red in the face when you ask them to show an example of how a trigger can be called from a procedure.
It's the same silliness of saying that you have a sequence associated with a table.
|
|
|
|