Solved

Perform execute immediate within a trigger

Posted on 2008-10-17
8
1,735 Views
Last Modified: 2013-12-19
Can I do excute an sql dynamically within a trigger. I have an execute immediate statement as given below:

CREATE OR REPLACE TRIGGER MYTRIGGER
AFTER DELETE OR INSERT OR UPDATE
ON MYTABLE
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
  v_SQL VARCHAR2(15000);
BEGIN
  v_SQL := myPkg.getViewSQL;
  EXECUTE IMMEDIATE v_SQL;
END MYTRIGGER
/

All I am doing is getting a sql (which is a create or replace view statement) and trying to execute it within the trigger.  

I am getting an error
ORA-00936: missing expression
ORA-06512: at "MY_SCHEMA.MY_TRIGGER", line 5
ORA-04088: error during execution of trigger "MY_SCHEMA.MY_TRIGGER"

Any help is appreciated
0
Comment
Question by:subirc
8 Comments
 
LVL 6

Accepted Solution

by:
Jankovsky earned 150 total points
ID: 22747538
There are 2 issues:
1. you have to use pragma autonomous transaction to use DDL command (such as Create or replace view) within trigger;
2. check value returned by the myPkg.getViewSQL function - current error has been caused by mistake in the returned statement.
0
 
LVL 7

Expert Comment

by:DiscoNova
ID: 22747908
As Jankovsky said in #2, you should check what myPkg.getViewSQL function returns. The error message seems to indicate that the function returns nothing (ie. you're trying to execute an empty statement).

And the reason DDL is not allowed within a trigger (Jankovsky's #1) without the suggested pragma is that DDL has an implied COMMIT (both before and after the execution of the provided statement), which is not allowed within a trigger.
0
 

Author Comment

by:subirc
ID: 22760182
It's not a DDL, its a DML. Correct me if I am wrong. Create or replace view is DML, and DDLs are when you create a table/index.constaint etc.
However, I did add the pragma autonomous transaction to the trigger and still getting the same error, and myPkg.getViewSQL  returns a valid view; which if I call outside the trigger runs and compiles

subirc
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22760438
>Create or replace view is DML
wrong. CREATE, ALTER, DROP, TRUNCATE are all DDL statements.
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:subirc
ID: 22786598
Jankovsky,

You are right in both of your points. I need the PRAGMA AUTONOMOUS TRANSACTION and my getViewsql was not returning two view DDLs i.e. I had two  CREATE OR REPLACE VIEW statements returned from the  myPkg.getViewSQL function. Now, I have two functions  myPkg.getViewSQL1 and myPkg.getviewSQL2 and now they run fine.
So,  I tried this script and it works now
------------------------------------------------------------------------------------------------------
declare
   myresult VARCHAR2(30000);
begin
    myresult :=  myPkg.getViewSQL1;
    execute immediate myresult;       
    myresult :=  myPkg.getViewSQL2;
   execute immediate myresult;       
end;
/
---------------------------------------------------------------------------------------------------------------
Now, putting this in the trigger though, I am running into permissions problem. Looks like I don't have permission to execute the DDL from a trigger something which I don't have control on.  So I decided to write a  job instead using DMBS_JOB package. How do I write a job that runs everytime the table is inserted/updated/deleted? Or, I cannot do that and have to run the job every two minutes.
Also, how do I go about doing it ? I don't have much experience with DBMS_JOB package.

Will this work ?
------------------------------------------------------------------------------------------------------------------------
DECLARE
  X NUMBER;
BEGIN
  SYS.DBMS_JOB.SUBMIT
    (
      job        => X,
     what       =>   ' declare
   myresult VARCHAR2(30000);
begin
    myresult :=  myPkg.getViewSQL1;
    execute immediate myresult;       
    myresult :=  myPkg.getViewSQL2;
   execute immediate myresult;       
end; ' ,
     next_date  => to_date('23/10/2008 09:34:44','dd/mm/yyyy hh24:mi:ss'),
     interval   => 'SYSDATE+1/3600',
     no_parse   => FALSE,
    );
END;
---------------------------------------------------------------------------------------------------------------------
Jankovsky,  thank you, you already earned the initial points, I am raising the points now.

subirc
0
 

Author Comment

by:subirc
ID: 22786892
Sorry increasing the points
0
 

Author Closing Comment

by:subirc
ID: 31507270
I will put up a new thread since I asked a new question
0
 

Author Comment

by:subirc
ID: 22796900
I will be posting a new thread as I changed the question.
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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

744 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

15 Experts available now in Live!

Get 1:1 Help Now