Trigger mysql - error?

0

Hello, this is the first time I'm using triggers and I do not know how to solve a bug I'm having and a problem I do not know how to do.

I want to make a trigger for when an inserted date (start and end date) is superimposed between a date that already exists .. ie if a date A to D already exists, a date B to C can not be inserted .. or C to E because they overlap ..

My SELECT is already correct I think, however I do not know how to make this error if the dates are overlapped .. I already tried with raise error and other ways but it gives me always syntax error ..

DELIMITER $$
CREATE TRIGGER tri_check_date_overlap BEFORE INSERT ON Oferta
FOR EACH ROW 
BEGIN


    IF EXISTS(
        SELECT * FROM Oferta WHERE 
             (new.morada = morada AND new.codigo = codigo 
                AND ((new.data_inicio BETWEEN data_inicio AND data_fim) OR new.data_fim BETWEEN data_inicio AND data_fim)
             )
             )


            /*CALL raise_application_error(3001, 'erro!'); */
            DECLARE msg varchar(255); 
            set msg = concat('Error: overlaping dates!', cast(new.right as char));
            signal sqlstate '45000' set message_text = msg;




END $$
DELIMITER ;

What do I have to fix?

    
asked by anonymous 01.12.2016 / 22:01

0 answers