简体   繁体   中英

Syntax Error in Mysql Delete Trigger with conditions

I have written a small trigger function in MySQL . This is the trigger query i have written and give syntax error in code.

Error :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cp WHERE cp . customer_plan_id = first_id;

DELIMITER $$
CREATE TRIGGER delete_plan_on_delete_customer 
    AFTER DELETE ON customers
    FOR EACH ROW 
BEGIN
    DECLARE active_plan INT;
    DECLARE first_id INT;

    SET active_plan = (SELECT is_active
    FROM customer_plans 
    WHERE customer_id=OLD.customer_id AND first_plan_id=1);

    if(active_plan = 0)THEN

        SET first_id = (SELECT customer_plan_id
        FROM customer_plans 
        WHERE customer_id=OLD.customer_id AND first_plan_id=1);

        DELETE customer_plans cp WHERE `cp`.`customer_plan_id`= first_id;
   END IF; 
END$$
DELIMITER ;

Syntax of DELETE query, from MySQL documentation is:

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

Change your DELETE lines in the trigger to:

DELETE FROM customer_plans WHERE customer_plan_id = first_id;

Sidenote: Based on my personal experience, it is a good habit to DECLARE default values for the variables. eg:

DECLARE active_plan INT(11) DEFAULT 0;
DECLARE first_id INT(11) DEFAULT 0;

I need help with my SYNTAX error I try to add trigger into mysql by still have syntax error. I will be glad for any help

CREATE TRIGGER delete_przejazdy_logger AFTER DELETE ON przejazdy FOR EACH ROW
BEGIN
INSERT INTO zmiany (rodzaj, data_modyfikaji, uzytkownik,
    id_pasazera, imie_nazwisko, tel_1, tel_2, tel_3, email,
    id_odbiorcy, imie_nazwisko2, tel2_1, tel2_2, dokument2,
    id_adres_z, kraj_z, miejscowosc_z, adres_z, kod_z, tel_stac_z,
    id_adres_do, kraj_do, miejscowosc_do, adres_do, kod_do, tel_stac_do, 
    data, osob, fotelik, detale, ilosc_bagazu)
(SELECT 1, CURRENT_DATE, SUBSTRING_INDEX(USER(), '@', 1),
    pasazerowie.id AS id_pasazera, pasazerowie.imie_nazwisko, pasazerowie.tel_1, pasazerowie.tel_2, pasazerowie.tel_3, pasazerowie.email,
    odbiorcy.id AS id_odbiorcy, odbiorcy.imie_nazwisko AS imie_nazwisko2, odbiorcy.tel_1 AS tel2_1, odbiorcy.tel_2 AS tel2_2, odbiorcy.dokument AS dokument2,
    adresy_z.id AS id_adres_z, adresy_z.kraj AS kraj_z, adresy_z.miejscowosc AS miejscowosc_z, adresy_z.adres AS adres_z, adresy_z.kod AS kod_z, adresy_z.tel_stac AS tel_stac_z,
    adresy_do.id AS id_adres_do, adresy_do.kraj AS kraj_do, adresy_do.miejscowosc AS miejscowosc_do, adresy_do.adres AS adres_do, adresy_do.kod AS kod_do, adresy_do.tel_stac AS tel_stac_do,
    OLD.data, OLD.osob, OLD.fotelik, OLD.detale, OLD.ilosc_bagazu
FROM pasazerowie
LEFT JOIN pasazerowie odbiorcy ON OLD.id_odbiorcy = odbiorcy.id
JOIN adresy adresy_do ON OLD.id_adres_do = adresy_do.id
JOIN adresy adresy_z ON OLD.id_adres_z = adresy_z.id
WHERE pasazerowie.id = OLD.id_pasazera);
END;

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