希望每个人都很好。
我正在尝试创建一个mysql触发器但是我一直在使用 以下错误:
[Err] 1064 - 您的SQL语法有错误; 检查与您的MySQL服务器版本对应的手册 使用“DELIMITER'正确的语法”在第1行 [错误] DELIMITER;
我的代码如下(请注意,实际文件是130行,我刚刚包含问题所在的部分,或者至少我相信它。)
/* BB Events Insert After Trigger */
DELIMITER $$
CREATE TRIGGER `event_insert_after` AFTER INSERT ON `bb_events`
FOR EACH ROW
BEGIN
UPDATE `bb_device` SET `latitude` = NEW.`latitude`, `longitude` = NEW.`longitude`, `status_code` = NEW.`status_code`, `timestamp` = NEW.`timestamp`, `speed` = NEW.`speed`, `driver_id` = NEW.`driver_id`, `heading` = NEW.`heading` WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id`;
IF NEW.`status_code` = 62465 THEN
INSERT INTO `bb_journey` (`account_id`, `device_id`, `start_timestamp`, `start_street`, `start_city`, `start_state`, `start_country`, `start_latitude`, `start_longitude`, `start_geozone`, `start_odometer`) VALUES (NEW.`account_id`, NEW.`device_id`, NEW.`timestamp`, NEW.`street`, NEW.`city`, NEW.`state`, NEW.`country`, NEW.`latitude`, NEW.`longitude`, NEW.`geozone_id`, NEW.`odometer`);
UPDATE `bb_device` SET `current_journey` = (SELECT `id` FROM `bb_journey` WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id` ORDER BY `id` DESC LIMIT 1) WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id`;
END IF;
IF NEW.`status_code` = 62467 THEN
UPDATE `bb_journey` SET `end_timestamp` = NEW.`timestamp`, `end_street` = NEW.`street`, `end_city` = NEW.`city`, `end_state` = NEW.`state`, `end_country` = NEW.`country`, `end_latitude` = NEW.`latitude`, `end_longitude` = NEW.`longitude`, `end_geozone` = NEW.`geozone_id`, `end_odometer` = NEW.`odometer` WHERE `id` = (SELECT `current_journey` FROM `bb_device` WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id` ORDER BY `id` DESC LIMIT 1);
UPDATE `bb_device` SET `current_journey` = '' WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id`;
END IF;
END;$$
DELIMITER ;
/* BB Journey Update After Trigger */
DELIMITER $$
CREATE TRIGGER `journey_update_before` BEFORE UPDATE ON `bb_journey`
FOR EACH ROW
BEGIN
IF NEW.`complete` <> 1 THEN
SET NEW.`distance` = ROUND(NEW.`end_odometer`-NEW.`start_odometer`,2);
SET NEW.`duration` = SEC_TO_TIME(NEW.`end_timestamp`-NEW.`start_timestamp`);
END IF;
IF NEW.`complete` = 1 THEN
UPDATE `bb_events` SET `journey_id` = NEW.`id` WHERE `timestamp` BETWEEN NEW.`start_timestamp` AND NEW.`end_timestamp`;
END IF;
END;$$
DELIMITER ;
非常感谢任何帮助!
谢谢,
Smithey:)
答案 0 :(得分:0)
在查看Mysql create trigger 1064 error
后我想出了这个正确的代码如下:
/* BB Events Insert After Trigger */
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `event_insert_after` $$
CREATE TRIGGER `event_insert_after` AFTER INSERT ON `bb_events`
FOR EACH ROW
BEGIN
UPDATE `bb_device` SET `latitude` = NEW.`latitude`, `longitude` = NEW.`longitude`, `status_code` = NEW.`status_code`, `timestamp` = NEW.`timestamp`, `speed` = NEW.`speed`, `driver_id` = NEW.`driver_id`, `heading` = NEW.`heading` WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id`;
IF NEW.`status_code` = 62465 THEN
INSERT INTO `bb_journey` (`account_id`, `device_id`, `start_timestamp`, `start_street`, `start_city`, `start_state`, `start_country`, `start_latitude`, `start_longitude`, `start_geozone`, `start_odometer`) VALUES (NEW.`account_id`, NEW.`device_id`, NEW.`timestamp`, NEW.`street`, NEW.`city`, NEW.`state`, NEW.`country`, NEW.`latitude`, NEW.`longitude`, NEW.`geozone_id`, NEW.`odometer`);
UPDATE `bb_device` SET `current_journey` = (SELECT `id` FROM `bb_journey` WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id` ORDER BY `id` DESC LIMIT 1) WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id`;
END IF;
IF NEW.`status_code` = 62467 THEN
UPDATE `bb_journey` SET `end_timestamp` = NEW.`timestamp`, `end_street` = NEW.`street`, `end_city` = NEW.`city`, `end_state` = NEW.`state`, `end_country` = NEW.`country`, `end_latitude` = NEW.`latitude`, `end_longitude` = NEW.`longitude`, `end_geozone` = NEW.`geozone_id`, `end_odometer` = NEW.`odometer` WHERE `id` = (SELECT `current_journey` FROM `bb_device` WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id` ORDER BY `id` DESC LIMIT 1);
UPDATE `bb_device` SET `current_journey` = '' WHERE `account_id` = NEW.`account_id` AND `device_id` = NEW.`device_id`;
END IF;
END;
$$
/* BB Journey Update After Trigger */
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `journey_update_before` $$
CREATE TRIGGER `journey_update_before` BEFORE UPDATE ON `bb_journey`
FOR EACH ROW
BEGIN
IF NEW.`complete` <> 1 THEN
SET NEW.`distance` = ROUND(NEW.`end_odometer`-NEW.`start_odometer`,2);
SET NEW.`duration` = SEC_TO_TIME(NEW.`end_timestamp`-NEW.`start_timestamp`);
END IF;
IF NEW.`complete` = 1 THEN
UPDATE `bb_events` SET `journey_id` = NEW.`id` WHERE `timestamp` BETWEEN NEW.`start_timestamp` AND NEW.`end_timestamp`;
END IF;
END;
$$
我不知道它是如何工作的,但确实如此。 MySQL版本5.5,CentOS 6 64位。
谢谢大家: - )