简体   繁体   English

使用MariaDB / MySQL持续获得1064

[英]Keep getting 1064 with MariaDB/MySQL

I'm struggling to get the below trigger to compile with my database. 我正在努力使下面的触发器与我的数据库一起编译。 Right now its MariaDB v5.5.56. 现在,其MariaDB v5.5.56。 I have tried this syntax in online validators and here are the results: 我已经在在线验证器中尝试了这种语法,结果如下:

SQLFiddle: SQLFiddle:

  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 '' at line 4

https://www.eversql.com/sql-syntax-check-validator/ https://www.eversql.com/sql-syntax-check-validator/

 Great work, the query's syntax is valid!

https://rextester.com/l/mysql_online_compiler https://rextester.com/l/mysql_online_compiler

 Compiles with no errors

In my environment I get: 在我的环境中,我得到:

 Error Code: 1064. 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 '' at line 4.

My error seems to match the SQLFiddle error...but its odd the other parsers worked just fine. 我的错误似乎与SQLFiddle错误匹配...但是其他解析器工作正常也很奇怪。 Maybe there is something wrong with my syntax based on my DB version? 基于数据库版本的语法可能有问题吗?

Here is what I am trying to execute: 这是我要执行的操作:

 CREATE TRIGGER insert_pad
 BEFORE INSERT on vehicles FOR EACH ROW
 BEGIN
 DECLARE done INT DEFAULT FALSE;
 DECLARE localid CHAR(17);
 DECLARE veh_mod CHAR(17);
 DECLARE cur1 CURSOR FOR select id, RPAD(vin, 17,'Q') from   vehicles where length(rtrim(vin)) < 17;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

 OPEN cur1;

 read_loop: LOOP
 FETCH cur1 INTO localid, veh_mod;

 IF done THEN
  LEAVE read_loop;
 END IF;
 UPDATE vehicles SET vin = veh_mod WHERE id = localid;


 END LOOP;

 CLOSE cur1;

 END;

If anyone has any thoughts I'd love to hear them! 如果有人有任何想法,我很想听听他们的意见! Thanks again! 再次感谢!

I'm not seeing a change to the delimiter. 我没有看到定界符的变化。 Without that, the semicolon on line 4 is going to terminate the statement, and that's going to throw an error. 否则,第4行的分号将终止该语句,这将引发错误。

The DELIMITER statement changes the statement delimiter. DELIMITER语句更改语句定界符。 We want to change to that to some string that doesn't appear in the statement we want to execute. 我们想要将其更改为一些我们想要执行的语句中未出现的字符串。 In this example, we changing the delimiter to two dollar signs, and then changing it back to the default semicolon. 在此示例中,我们将分隔符更改为两个美元符号,然后将其更改回默认的分号。

DELIMITER $$

CREATE TRIGGER insert_pad
BEFORE INSERT on vehicles FOR EACH ROW
BEGIN
  DECLARE done INT DEFAULT FALSE;
  DECLARE localid CHAR(17);
  ...
END$$

DELIMITER ;

Another thought: I don't think a trigger is allowed to issue DML (insert/update/delete) against a table that is referenced in the statement that causes the trigger to be fired. 另一个想法:我认为触发器不允许针对导致触发器被触发的语句中引用的表发出DML(插入/更新/删除)。

But in a BEFORE INSERT trigger, we are allowed to reference and modify values of columns of the row that is about to be inserted. 但是在INSERT触发器之前,我们被允许引用和修改将要插入的行的列的值。 We use the special qualifier NEW. 我们使用特殊的限定词NEW. to reference columns in the row being inserted. 引用要插入的行中的列。 For example: 例如:

 DELIMITER $$

 CREATE TRIGGER insert_pad
 BEFORE INSERT ON vehicles FOR EACH ROW
 BEGIN
   -- if vin less than 17 characters, pad with Q  
   IF( CHAR_LENGTH(TRIM( NEW.vin )) < 17 ) THEN
     SET NEW.vin = RPAD(TRIM( NEW.vin ), 17,'Q');
   END IF;
 END$$

 DELIMITER ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM