php – 在触发器中使用分隔符的Mysql错误

这是我的mysql触发器语句

DELIMITER |
CREATE TRIGGER kk AFTER UPDATE
    ON location FOR EACH ROW
 BEGIN
        IF NEW.name not in(SELECT A.name FROM filter A  WHERE (NEW.name = A.name))THEN  

        IF NEW.location_name != OLD.location_name THEN
            INSERT INTO filter(old_location_name,new_location_name) 
            VALUES (OLD.location_name, NEW.location_name);

        ELSE

                UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name;  

        END IF; 

        ELSE

            UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name;


        END IF; 

  END;|
DELIMITER ;

当我在phpMyadmin中粘贴此代码时,这很好用.

在我的项目中

$sqlDrop = "DROP TRIGGER IF EXISTS `kk`";       
$resDrop = parent::_executeQuery($sqlDrop); 

echo $sql = "same above delimiter trigger query";
$rs = parent::_executeQuery($sql );

现在$sql语句在一行中回声

 DELIMITER | CREATE TRIGGER kk AFTER UPDATE ON location FOR EACH ROW BEGIN IF NEW.name not in(SELECT A.name FROM filter A  WHERE (NEW.name = A.name))THEN IF NEW.location_name != OLD.location_name THEN INSERT INTO filter(old_location_name,new_location_name,name) VALUES (OLD.location_name, NEW.location_name);    ELSE UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name; END IF; ELSE UPDATE filter SET old_location_name = OLD.location_name , new_location_name = NEW.location_name WHERE name = OLD.name; END IF; END;| DELIMITER ;

并且不执行查询.当我在我的phpMyadmin中粘贴echoed语句时,它显示我的错误

php  – 在触发器中使用分隔符的Mysql错误

那可能是什么问题.这是执行触发器查询的正确方法吗?

解决方法:

我不确定这对你是否真的有帮助.如何单独执行每个查询?

$startDelimit = 'DELIMITER |';
$resStart = parent::_executeQuery($startDelimit);

然后是你的create trigger语句

$sql = "CREATE TRIGGER kk AFTER UPDATE ...... END IF; END;";
$rs = parent::_executeQuery($sql );

最后的声明

$endDelimit = '| DELIMITER ;';
$resEnd = parent::_executeQuery($endDelimit );
上一篇:MySQL数据版本控制


下一篇:mysql – 处理ON INSERT触发器时如何锁定innodb表?