Solved

Creating trigger in Oracle

Posted on 2008-06-22
6
1,024 Views
Last Modified: 2013-12-07
I need to create a script that checks is a list of triggers exists in the database and if so drop then and the create new ones with the new specs for that triggers. I'm trying this
DECLARE cnt NUMBER;
BEGIN  
  select count(*) into cnt from user_triggers where trigger_name = 'TIB_DCMS_HEADERS';
  IF( cnt = 1 )  THEN    
    execute immediate 'DROP TRIGGER TIB_DCMS_HEADERS';
  END IF;
END;
CREATE TRIGGER TIB_DCMS_HEADERS BEFORE INSERT
  ON DCMS_HEADERS FOR EACH ROW
BEGIN
  SELECT S_DCMS_HEADERS.NEXTVAL INTO NEW.ID FROM DUAL;
END;
But i'm getting an error saying:
ORA-06550: line 8, column 1:
PLS-00103: Encountered the symbol "CREATE"
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

I need to replicate this to the other triggers that I have. Thanks for the help.
0
Comment
Question by:nikeman_1479
  • 3
  • 3
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21840290
you will need to put the CREATE TRIGGER also inside a EXECUTE IMMEDIATE
DECLARE cnt NUMBER;
BEGIN  
  select count(*) into cnt from user_triggers where trigger_name = 'TIB_DCMS_HEADERS';
  IF( cnt = 1 )  THEN    
    execute immediate 'DROP TRIGGER TIB_DCMS_HEADERS';
  END IF;
END;
EXECUTE IMMEDIATE 'CREATE TRIGGER TIB_DCMS_HEADERS BEFORE INSERT
  ON DCMS_HEADERS FOR EACH ROW
BEGIN
  SELECT S_DCMS_HEADERS.NEXTVAL INTO NEW.ID FROM DUAL;
END;
';

Open in new window

0
 

Author Comment

by:nikeman_1479
ID: 21840315
Hi, I already tried something similar, but I get this error now: ORA-06550: line 8, column 1:
PLS-00103: Encountered the symbol "EXECUTE"
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
The tool that I'm using is the Oracle SQL Developer
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21840325
sorry, the END must be after the EXECUTE IMMEDIATE.
DECLARE cnt NUMBER;
BEGIN  
  select count(*) into cnt from user_triggers where trigger_name = 'TIB_DCMS_HEADERS';
  IF( cnt = 1 )  THEN    
    execute immediate 'DROP TRIGGER TIB_DCMS_HEADERS';
  END IF;
EXECUTE IMMEDIATE 'CREATE TRIGGER TIB_DCMS_HEADERS BEFORE INSERT
  ON DCMS_HEADERS FOR EACH ROW
BEGIN
  SELECT S_DCMS_HEADERS.NEXTVAL INTO NEW.ID FROM DUAL;
END;
';
END;

Open in new window

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:nikeman_1479
ID: 21840490
Hi, again, now I got this erro: ORA-24344: success with compilation error
ORA-06512: at line 7
24344. 00000 -  "success with compilation error"
*Cause:    A sql/plsql compilation error occurred.
*Action:   Return OCI_SUCCESS_WITH_INFO along with the error code
Which I run in to already but could found a solution to it, thanks for helping, I have run of ideas
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 400 total points
ID: 21840507
what about this:
DECLARE cnt NUMBER;
BEGIN  
  select count(*) into cnt from user_triggers where trigger_name = 'TIB_DCMS_HEADERS';
  IF( cnt = 1 )  THEN    
    execute immediate 'DROP TRIGGER TIB_DCMS_HEADERS';
  END IF;
EXECUTE IMMEDIATE 'CREATE TRIGGER TIB_DCMS_HEADERS BEFORE INSERT
  ON DCMS_HEADERS FOR EACH ROW
BEGIN
  SELECT S_DCMS_HEADERS.NEXTVAL INTO :NEW.ID FROM DUAL;
END;
';
END;

Open in new window

0
 

Author Comment

by:nikeman_1479
ID: 21840541
It is alive thanks a lot, it resolve me a big issue
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
Query Records that don't match 8 42
PL/SQL Display based on value 4 26
exp/imp 25 73
Extract Currency data from a string and put them in a new field 3 36
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 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 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…

776 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