我的Mysql触发器有什么问题 - 抛出语法错误

时间:2013-01-19 07:19:16

标签: mysql triggers

当我执行此触发器时,它会抛出错误

#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第27行的“END”附近使用正确的语法

delimiter //

CREATE TRIGGER `after_tblgps_insert` AFTER INSERT ON `tbl_gps`
FOR EACH ROW BEGIN
DECLARE cab_meter_new VARCHAR(10);
DECLARE cab_cost_new VARCHAR(10);
DECLARE cab_meter_old VARCHAR(10);
DECLARE cab_cost_old VARCHAR(10);
DECLARE pr_pnr_no  VARCHAR(15);
DECLARE pr_dest_id  VARCHAR(15);

select NEW.gps_rs232,NEW.gps_cab_bus,gps_rs232,gps_cab_bus into @cab_meter_new,@cab_cost_new,@cab_meter_old,@cab_cost_old from tbl_gps where gps_imei = NEW.gps_imei order by gps_id asc limit 0,1;

IF @cab_meter_new = 1 and @cab_meter_old = 0 THEN
BEGIN
select `booking_pnr_no` into @pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 13;
select trip_dst_area_id into @pr_dest_no from tbl_trip where pnr_id = @pr_pnr_no order by trip_id desc limit 0,1;
update tbl_taxi set taxi_cur_status = 12,taxi_cur_area = @pr_dest_no where taxi_id = NEW.gps_cab_bus;
update tbl_booking set booking_status = 9 where booking_pnr_no = @pr_pnr_no;
END;
ELSE IF @cab_meter_new = 0 and @cab_meter_old = 1 THEN
BEGIN
select `booking_pnr_no` into @pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 9;
select trip_dst_area_id into @pr_dest_no from tbl_trip where pnr_id = @pr_pnr_no order by trip_id desc limit 0,1;
update tbl_taxi set taxi_cur_status = 10,taxi_cur_area = @pr_dest_no where taxi_id = NEW.gps_cab_bus;
update tbl_booking set booking_status = 8 where booking_pnr_no = @pr_pnr_no;
END;
END IF
END //
delimiter ;

2 个答案:

答案 0 :(得分:1)

试试这段代码

delimiter //

CREATE TRIGGER `after_tblgps_insert` AFTER INSERT ON `tbl_gps`
FOR EACH ROW BEGIN
DECLARE cab_meter_new VARCHAR(10);
DECLARE cab_cost_new VARCHAR(10);

    DECLARE cab_meter_old VARCHAR(10);
    DECLARE cab_cost_old VARCHAR(10);
    DECLARE pr_pnr_no  VARCHAR(15);
    DECLARE pr_dest_id  VARCHAR(15);

    select NEW.gps_rs232,NEW.gps_cab_bus,gps_rs232,gps_cab_bus into @cab_meter_new,@cab_cost_new,@cab_meter_old,@cab_cost_old from tbl_gps where gps_imei = NEW.gps_imei order by gps_id asc limit 0,1;

    IF @cab_meter_new = 1 and @cab_meter_old = 0 THEN
    BEGIN
    select `booking_pnr_no` into @pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 13;
    select trip_dst_area_id into @pr_dest_no from tbl_trip where pnr_id = @pr_pnr_no order by trip_id desc limit 0,1;
    update tbl_taxi set taxi_cur_status = 12,taxi_cur_area = @pr_dest_no where taxi_id = NEW.gps_cab_bus;
    update tbl_booking set booking_status = 9 where booking_pnr_no = @pr_pnr_no;
    END;
    END IF;
    IF @cab_meter_new = 0 and @cab_meter_old = 1 THEN//break IF into separate IF 
    BEGIN
    select `booking_pnr_no` into @pr_pnr_no from tbl_booking where booking_alloted_vehicle = (select taxi_id from tbl_taxi where taxi_imei = NEW.gps_imei ) AND booking_status = 9;
    select trip_dst_area_id into @pr_dest_no from tbl_trip where pnr_id = @pr_pnr_no order by trip_id desc limit 0,1;
    update tbl_taxi set taxi_cur_status = 10,taxi_cur_area = @pr_dest_no where taxi_id = NEW.gps_cab_bus;
    update tbl_booking set booking_status = 8 where booking_pnr_no = @pr_pnr_no;
    END;
    END IF;
    END; //
    delimiter ;

答案 1 :(得分:0)

END IF之后你有错误的分号。