Solved

Perform execute immediate within a trigger

Posted on 2008-10-17
8
1,739 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
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!

 
LVL 143

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Distinct values from all columns in a table?? PL SQL 4 58
Oracle DBLINKS From 11g to 8i 3 67
error in my cursor 5 50
Updating a temp table inside a PL/SQL block 3 51
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

733 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