Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Perform execute immediate within a trigger

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

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  â€¦
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

860 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