Solved

ORA-04098: trigger is invalid and failed re-validation

Posted on 2014-07-21
8
5,578 Views
Last Modified: 2014-07-21
I'm trying to add an auto increment field in oracle using a trigger. I used this article as a reference: http://stackoverflow.com/questions/11296361/how-to-create-id-with-auto-increment-on-oracle

It created the sequence (though it did not store it in the table script) and created the trigger, but when I try to add a new row to the table (either manually or using an insert script) I get this error:

ORA-04098: trigger is invalid and failed re-validation
0
Comment
Question by:pippen
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 73

Expert Comment

by:sdstuber
ID: 40209045
The sequence won't be part of the table script because the sequence is its own object.

The trigger in that link is valid for the table and sequence defined in that link.

If your trigger has an error, please post your table DDL and trigger code.

Also, what version of the database are you using?  In 12c you can create an auto-increment column without the trigger.
0
 

Author Comment

by:pippen
ID: 40209084
We have Oracle 9.5

CREATE TABLE xx.CEP_DCS
(
  CEP_ID                   NUMBER               NOT NULL,
  STUDENT_KEY              NUMBER,
  ISP_ID                   NUMBER,
  COUNTY_CODE              VARCHAR2(2 BYTE),
  SSN                      NUMBER,
  FIRST_NAME               VARCHAR2(15 BYTE),
  MIDDLE_INITIAL           VARCHAR2(1 BYTE),
  LAST_NAME                VARCHAR2(15 BYTE),
  CASE_NUMBER              VARCHAR2(10 BYTE),
  DATE_OF_BIRTH            DATE,
  GUARDIAN_FIRST_NAME      VARCHAR2(15 BYTE),
  GUARDIAN_MIDDLE_INITIAL  VARCHAR2(1 BYTE),
  GUARDIAN_LAST_NAME       VARCHAR2(15 BYTE),
  ADDRESS                  VARCHAR2(71 BYTE),
  CITY                     VARCHAR2(20 BYTE),
  STATE                    VARCHAR2(2 BYTE),
  ZIP                      VARCHAR2(5 BYTE),
  CERTIFIED_FOSTER_YN      VARCHAR2(1 BYTE)
)
TABLESPACE xx
PCTUSED    0
PCTFREE    10
INITRANS   1
MAXTRANS   255
STORAGE    (
            INITIAL          64K
            MINEXTENTS       1
            MAXEXTENTS       UNLIMITED
            PCTINCREASE      0
            BUFFER_POOL      DEFAULT
           )
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;


CREATE OR REPLACE TRIGGER xx.cep_dcs_trig
BEFORE INSERT ON xx.CEP_DCS FOR EACH ROW
BEGIN
  SELECT cep_dcs_seq.NEXTVAL
  INTO   :new.cep_id
  FROM   dual;
END
/

-----------------------------------------------------------------------

CREATE OR REPLACE TRIGGER cep_dcs_trig
BEFORE INSERT ON cep_dcs
FOR EACH ROW
BEGIN
  SELECT cep_dcs_seq.NEXTVAL
  INTO   :new.cep_id
  FROM   dual;
END
/
0
 
LVL 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 150 total points
ID: 40209101
Does the sequence "cep_dcs_seq" even exists?!
btw: why do you create the trigger "twice" (CREATE OR REPLACE TRIGGER cep_dcs_trig )?!
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 200 total points
ID: 40209102
9.5 isn't a valid oracle database version.

Your trigger should have a semicolon after the END

CREATE OR REPLACE TRIGGER cep_dcs_trig
BEFORE INSERT ON cep_dcs
FOR EACH ROW
BEGIN
  SELECT cep_dcs_seq.NEXTVAL
  INTO   :new.cep_id
  FROM   dual;
END;
/ 

Open in new window


also, you need to create the cep_dcs_seq sequence
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:pippen
ID: 40209611
Sorry, that was Toad. It's Oracle 10G.

ok. That fixed the trigger. (missing semi-colon.) Thanks!

My load script runs now, but it doesn't insert anything. The table is still empty. (after committing and refreshing.)

Here is the load script that now runs without error, but does not insert anything:

insert into CEP_DCS
(select
null cep_id,
null student_key,
null isp_id,
trim(substr(DATAROW,1,2)),
trim(substr(DATAROW,3,9)),
upper(trim(substr(DATAROW,12,15))),
upper(trim(substr(DATAROW,27,1))),
upper(trim(substr(DATAROW,28,15))),
trim(substr(DATAROW,43,10)),
TO_DATE(trim(substr(DATAROW,55,4) || substr(DATAROW,53,2)),'MMDDRR'),
null,  --upper(trim(substr(DATAROW,59,15))),
null,  --upper(trim(substr(DATAROW,74,1))),
null,  --upper(trim(substr(DATAROW,75,15))),
null,  --upper(trim(substr(DATAROW,90,71))),
upper(trim(substr(DATAROW,161,20))),
upper(trim(substr(DATAROW,181,2))),
upper(trim(substr(DATAROW,183,5))),
'Y'
from CEP_DCS_RAW
--where rownum < 20
);
0
 
LVL 16

Assisted Solution

by:Wasim Akram Shaik
Wasim Akram Shaik earned 150 total points
ID: 40209658
Is the select retrieving data??.. Check the select statement output..!!!
0
 

Author Comment

by:pippen
ID: 40209660
Another problem is that I had the cep_id set to NOT NULL, and had to change that, and I was working in a copy of the database where the table I was loading from was empty.

I think it should work now. I will post back and award points when it is done.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40209683
Just as a sidenote / btw: if the table/view "CEP_DCS_RAW" is based on a flat file, like a CSV, using SQLLDR would definitely be faster and more suitable for this kind of data loading...
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

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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…
Via a live example, show how to take different types of Oracle backups using RMAN.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

758 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

21 Experts available now in Live!

Get 1:1 Help Now