Solved

Perform execute immediate within a trigger

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Migrating an SQL 2008 database to Oracle 12c 3 99
oracle 11g 23 73
constraint check 2 40
Where Does Time Value Come From for Database Insert or Update 4 26
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.

813 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

12 Experts available now in Live!

Get 1:1 Help Now