Error when trying to insert data into a table (Trigger)

1

I am new here and I come to ask you where is the error in the code (exact match returns ..) since I am eating my head and I still can not find it. It turns out that the Trigger works with the first two inserts in the Call table but in the third it returns the error that I leave below. I want the trigger to control this restriction: The year of the closing date of the call must be one year higher than the closing date of the call. Thank you very much!

CREATE TABLE ALUMNO_US(
OID_AL  number(8)   NOT NULL PRIMARY KEY,
dni         CHAR(9) NOT NULL CHECK (LENGTH(dni)=9),
nombre      VARCHAR2(50) ,
apellido    VARCHAR2(50) ,
email       VARCHAR2(50) check (email LIKE '%@%'),
telefono    INTEGER,
fechaNacimiento DATE
);


CREATE TABLE CONVOCATORIA(
    OID_CON number(8) NOT NULL PRIMARY KEY,
    numero  INTEGER NOT NULL,
    tipo    VARCHAR2(20) NOT NULL,
    fechaApertura DATE,
    fechaCierre   DATE,
    CONSTRAINT chk_fechaCierre check(fechaCierre > fechaApertura),
    OID_AL  number(8),
    OID_AC number(8),
    FOREIGN KEY (OID_AL) REFERENCES ALUMNO_US ON DELETE CASCADE,
    FOREIGN KEY (OID_AC) REFERENCES ALUMNOSCONVOCATORIA ON DELETE CASCADE
     );

//////// DELETING TABLES DATA AND CREATING TRIGGER ///////

delete alumno_us;
delete convocatoria;

create or replace trigger fechaCon
after insert  ON convocatoria
declare
fechaA number;
fechaC number;
begin
select extract(year from fechaApertura) into fechaA from convocatoria;
select extract(year from fechaCierre) into fechaC from convocatoria;

if (fechaC != fechaA + 1)
then raise_application_error (-20600, ' El año de la fecha de cierre de la convocatoria tiene que ser superior en una unidad al año de la fecha de cierre de dicha convocatoria');
end if;
end;
/
alter trigger fechaCon enable;

///// INSERTION OF TABLES //////

insert into alumno_us values ('1', '897453621', 'Paco', 'Burro', '[email protected]','786765123', to_date('03/03/1997','DD/MM/YYYY'));
insert into alumno_us values ('2', '897453621', 'Paco', 'Burro', '[email protected]','786765123', to_date('03/03/1998','DD/MM/YYYY'));
insert into alumno_us values ('3', '89745321', 'Paco', 'Burro', '[email protected]','786765123', to_date('03/03/1999','DD/MM/YYYY'));
insert into convocatoria values ('1', '78123', 'ERASMUS', to_date('09/12/1999','DD/MM/YYYY'),to_date('12/12/2001','DD/MM/YYYY'),'1', null);
insert into convocatoria values ('2', '78123', 'ERASMUS', to_date('11/12/1999','DD/MM/YYYY'),to_date('12/12/2000','DD/MM/YYYY'),'2', null);
insert into convocatoria values ('3', '78122', 'TRAINING', to_date('10/12/1995','DD/MM/YYYY'),to_date('12/12/2002','DD/MM/YYYY'),'3', null);

/// LOG ///

3 filas eliminado


0 filas eliminado


Trigger FECHACON compilado


Trigger FECHACON alterado.


1 fila insertadas.


1 fila insertadas.


1 fila insertadas.


Error que empieza en la línea: 22 del comando :
insert into convocatoria values ('1', '78123', 'ERASMUS', to_date('09/12/1999','DD/MM/YYYY'),to_date('12/12/2001','DD/MM/YYYY'),'1', null)
Informe de error -
ORA-20600:  El año de la fecha de cierre de la convocatoria tiene que ser superior en una unidad al año de la fecha de cierre de dicha convocatoria
ORA-06512: at "PRUEBAS.FECHACON", line 9
ORA-04088: error during execution of trigger 'PRUEBAS.FECHACON'


1 fila insertadas.


Error que empieza en la línea: 24 del comando :
insert into convocatoria values ('3', '78122', 'TRAINING', to_date('10/12/1995','DD/MM/YYYY'),to_date('12/12/2002','DD/MM/YYYY'),'3', null)
Informe de error -
ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at "PRUEBAS.FECHACON", line 5
ORA-04088: error during execution of trigger 'PRUEBAS.FECHACON'

The OID_AC is null because I have not created another table, which is not necessary to verify the correct functioning of the trigger

    
asked by Trillante 13.01.2018 в 14:37
source

1 answer

1

This trigger is after insert. Therefore the table already has the data inserted.

begin
select extract(year from fechaApertura) into fechaA from convocatoria;
select extract(year from fechaCierre) into fechaC from convocatoria;

These two lines, return a value from the call table. In the first case, the table has only one record therefore it walks. But when making the second correct insert, the table already has more than one record, therefore that select does not return a single value, but returns all of the table.

Those select is missing a where to find out which record you recently inserted.

You should add something like

where OID_CON = :new.OID_CON 
    
answered by 13.01.2018 / 17:55
source