触发器如果语句语法错误

时间:2014-09-11 11:36:12

标签: php mysql

我是MySQL PHP WAMP的新用户,我的触发器是在没有if语句的情况下成功创建的,它在执行时会出错。错误是:

  

1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'IF(NEW.userloc_lat = '91 .000000'和NEW.userloc_lan ='181.000000')附近使用正确的语法,然后在第6行使用

DELIMITER $$
CREATE TRIGGER before_new_userloc
    BEFORE INSERT ON tbl_userloc
    FOR EACH ROW BEGIN

    INSERT INTO tbl_unknown_site
    IF (NEW.userloc_lat ='91.000000' AND NEW.userloc_lan = '181.000000')
    THEN SET 
    unknown_site_lat = NEW.userloc_lat,
    unknown_site_lon = NEW.userloc_lon,
    unknown_site_stt = NOW(),
    unknown_site_dtm = NEW.userloc_dtm,
    unknown_site_appid = NEW.userloc_appid,
    unknown_site_cellid = NEW.userloc_cellid,
    unknown_site_lacid = NEW.userloc_lacid,
    unknown_site_subid = NEW.userloc_id;

END$$
DELIMITER ;

我认为只有我会看到的是syntanx错误。

userloc_latuserloc_lon的数据类型为decimal(10,6)

1 个答案:

答案 0 :(得分:1)

IF应该在INSERT之前:

DELIMITER $$
CREATE TRIGGER before_new_userloc
    BEFORE INSERT ON tbl_userloc
    FOR EACH ROW BEGIN
        IF (NEW.userloc_lat ='91.000000' AND NEW.userloc_lon = '181.000000')
        THEN
            INSERT INTO tbl_unknown_site
            SET 
                unknown_site_lat = NEW.userloc_lat,
                unknown_site_lon = NEW.userloc_lon,
                unknown_site_stt = NOW(),
                unknown_site_dtm = NEW.userloc_dtm,
                unknown_site_appid = NEW.userloc_appid,
                unknown_site_cellid = NEW.userloc_cellid,
                unknown_site_lacid = NEW.userloc_lacid,
                unknown_site_subid = NEW.userloc_id;
        END IF;

    END$$
DELIMITER ;