简体   繁体   中英

Mysql trigger statement keeps giving syntax error

I keep getting error messages when I try to execute this statement in phpmyadmin. I'm running mysql 5.7

DELIMITER //
CREATE OR REPLACE TRIGGER update_counts_trigger AFTER INSERT OR UPDATE OR DELETE ON tickets
FOR EACH ROW
BEGIN
    DECLARE V_uitvoeringId, V_ReserveringId varchar(50);
    DECLARE V_tekoop, V_gereserveerd, V_wachtlijst int;
    
    SET V_ReserveringId = NEW.reserveringId OR OLD.reserveringId;
    SET V_uitvoeringId = ( SELECT uitvoeringId FROM reservering WHERE id=V_ReserveringId )
    SET V_tekoop = (
        SELECT count(*) 
        FROM tickets t 
        WHERE t.tekoop AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);

    SET V_gereserveerd =  (
        SELECT count(*) 
        FROM tickets t 
        WHERE NOT t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);

    SET V_wachtlijst = (
        SELECT count(*) FROM tickets t WHERE t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);

    update uitvoering 
        set tekoop = V_tekoop,       
            gereserveerd =  V_gereserveerd, 
            wachtlijst =  V_wachtlijst,
            vrije_plaatsen = aantal_plaatsen - V_gereserveerd + V_tekoop  
        WHERE id=V_uitvoeringId; 
END //
DELIMITER ;

Can anyone see what's wrong?

  • Unrecognized statement type. (near "DECLARE" at position 176)
  • This type of clause was previously parsed. (near "SET" at position 398)
  • Unrecognized statement type. (near "END" at position 1192)

There are other syntax errors causing those complaints.

  1. There is no OR REPLACE option with triggers in MySQL. The trigger must be dropped and then recreated.
  2. There is no OR ing with actions, only one, which means that you'll have to create 3 triggers even though the definition may be the same. However, it seems like you only need an AFTER UPDATE action? ... because AFTER INSERT does not have an OLD reference, and AFTER DELETE does not have a NEW reference.
  3. Getting past those two items, there is also a ; missing after the query with SELECT uitvoeringId .

Putting all that together, becomes:

DELIMITER //
CREATE TRIGGER update_counts_trigger AFTER UPDATE ON tickets
FOR EACH ROW
BEGIN
    DECLARE V_uitvoeringId, V_ReserveringId varchar(50);
    DECLARE V_tekoop, V_gereserveerd, V_wachtlijst int;
    
    SET V_ReserveringId = NEW.reserveringId OR OLD.reserveringId;
    SET V_uitvoeringId = ( SELECT uitvoeringId FROM reservering WHERE id=V_ReserveringId );
    SET V_tekoop = (
        SELECT count(*) 
        FROM tickets t 
        WHERE t.tekoop AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);

    SET V_gereserveerd =  (
        SELECT count(*) 
        FROM tickets t 
        WHERE NOT t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);

    SET V_wachtlijst = (
        SELECT count(*) FROM tickets t WHERE t.wachtlijst AND NOT t.geannuleerd AND NOT t.verkocht AND t.uitvoeringId = V_uitvoeringId);

    update uitvoering 
        set tekoop = V_tekoop,       
            gereserveerd =  V_gereserveerd, 
            wachtlijst =  V_wachtlijst,
            vrije_plaatsen = aantal_plaatsen - V_gereserveerd + V_tekoop  
        WHERE id=V_uitvoeringId; 
END //
DELIMITER ;

Then with that in phpMyAdmin:

在此处输入图片说明

Then after executing the SQL:

在此处输入图片说明

Then checking the triggers tab:

在此处输入图片说明

...and clicking Edit at the trigger shows the definition:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM