Trigger mysql - error?

Asked

Viewed 67 times

0

Hi, this is the first time I’m using triggers and I don’t know how to fix an error I’m having and a problem I don’t know how to do.

I want to do a Rigger for when a date entered (start date and end date) is superimposed between a date that already exists.. that is, if a date A to D already exists, a date B to C.. or C to E cannot be inserted because they overlap..

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

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 I have to fix?

  • What version of Mysql? Which I think only works in 5.5

  • 5.1.73. I don’t know if that’s it.. I was thinking it would be something to do with THEN.. Maybe an END IF.. Pq is a syntax error.. But I honestly don’t know.

  • Put the mistake you’re giving in the question

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.