Search code examples
sqloracle-databaseplsqldatabase-triggercreate-table

ERROR : PLS-00103: Encountered the symbol ";" when expecting one of the following: * & = - + < / > at in is mod remainder not rem then


This is a proyect for the University and I need some help :( What's the problem??

CREATE TABLE RESERVAS(
    OID_RE NUMBER NOT NULL,
    PrecioReserva INTEGER NOT NULL,
    fecha DATE NOT NULL,
    HoraInicio DATE NOT NULL,
    HoraFin DATE NOT NULL,
    OID_ESC NUMBER,
    OID_SOC NUMBER,
    OID_PIS NUMBER NOT NULL
);

CREATE OR REPLACE TRIGGER HorarioReseva
BEFORE INSERT OR UPDATE ON RESERVAS
FOR EACH ROW
DECLARE
    minutos NUMBER;
BEGIN 
    minutos := :NEW.HoraFin - :NEW.HoraInicio;
    IF(minutos > 120);
        THEN  raise_application_error
        ('No se puede reservar una pista durante más de 2h(120min)');
     END IF;
END;

Solution

  • Problems with your code:

    • there is a semi-colon at the end of this expression, which should not be there IF(minutos > 120); (note that the parentheses are superfluous, as commented by William Robertson)

    • raise_application_error() expects two arguments: an error number (between -20999 and -20000) and a message

    • substrating DATEs produces a number that represents their difference in days; as it is, your code ensures that the difference is less than 120 days, which, as far as concerned, is not what you want.

    Here is a correct version of your code:

    CREATE OR REPLACE TRIGGER HorarioReseva
    BEFORE INSERT OR UPDATE ON RESERVAS
    FOR EACH ROW
    DECLARE
        minutos NUMBER;
    BEGIN 
        minutos := :NEW.HoraFin - :NEW.HoraInicio;
        IF minutos > 2/24
            THEN  raise_application_error(
                -20000,
                'No se puede reservar una pista durante más de 2h(120min)'
            );
         END IF;
    END;
    /
    

    But basically: you want to ensure that the difference between HoraFin and HoraInicio is not greater than 2 hours. You don't need a trigger for this: a CHECK constraint is enough.

    CREATE TABLE RESERVAS (
        OID_RE NUMBER NOT NULL,
        PrecioReserva INTEGER NOT NULL,
        fecha DATE NOT NULL,
        HoraInicio DATE NOT NULL,
        HoraFin DATE NOT NULL,
        OID_ESC NUMBER,
        OID_SOC NUMBER,
        OID_PIS NUMBER NOT NULL,
        CHECK(HoraFin - HoraInicio <= 2/24)
    );
    

    Demo on DB Fiddle:

    insert into reservas (OID_RE, PrecioReserva, fecha, HoraInicio, HoraFin, OID_PIS)
    values(
        1, 
        2, 
        to_date('2019-01-01', 'yyyy-mm-dd'), 
        to_date('2019-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss'), 
        to_date('2019-01-01 02:00:00', 'yyyy-mm-dd hh24:mi:ss'),
        1
    );
    --> 1 rows affected
    
    insert into reservas (OID_RE, PrecioReserva, fecha, HoraInicio, HoraFin, OID_PIS)
    values(
        1, 
        2, 
        to_date('2019-01-01', 'yyyy-mm-dd'), 
        to_date('2019-01-01 00:00:00', 'yyyy-mm-dd hh24:mi:ss'), 
        to_date('2019-01-01 02:01:00', 'yyyy-mm-dd hh24:mi:ss'),
        1
    );
    --> ORA-02290: check constraint (FIDDLE_LTVQMOQEMVHALMINECEI.SYS_C00308788) violated