简体   繁体   English

MySQL触发器:#1064-您的SQL语法有误; 检查与您的MySQL服务器版本相对应的手册

[英]MySQL Trigger : #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version

I have problems creating triggers in MySQL 5.1.73 This is de syntax: 我在MySQL 5.1.73中创建触发器时遇到问题,这是de语法:

DELIMITER $$
CREATE TRIGGER `discount2` 
BEFORE 
INSERT ON `order_item` 
FOR EACH ROW 
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET @alumno := (SELECT user_id from `order` where `order`.id = NEW.order_id)
SET @profesor := (SELECT id_profesor from user where user.id = @alumno)
SET @dto := (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = @profesor)

SET.NEW.descuento = SELECT CAST((((@dto)*(NEW.pvp))/100) AS DECIMAL(10,2))
$$
DELIMITER ;

But there is some error... 但是有一些错误...

#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 'DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET @alu' at line 5

Someone can help me, please? 有人可以帮我吗? Thanks u 谢谢你

The syntax to create a BEFORE INSERT Trigger in MySQL is: 在MySQL中创建BEFORE INSERT触发器的语法为:

CREATE TRIGGER trigger_name
BEFORE INSERT
   ON table_name FOR EACH ROW

BEGIN

   -- variable declarations

   -- trigger code

END;

If you want to execute multiple statements in your trigger, you should use BEGIN and END . 如果要在触发器中执行多个语句 ,则应使用BEGINEND

The Reason for using DELIMITER $$ before and after the trigger declaration is to change the delimiter from ; 在触发器声明之前和之后使用DELIMITER $$的原因是将定界符从更改为; (default) to $$ . (默认)为$$

If you didn't change the DELIMITER , the first semicolon in your trigger code may be interpreted as the end of trigger declaration. 如果您没有更改DELIMITER ,则触发代码中的第一个分号可能会被解释为触发声明的结尾。 Thus the code following may result in syntax errors. 因此,以下代码可能会导致语法错误。

DELIMITER $$
CREATE TRIGGER `discount2` 
BEFORE 
INSERT ON `order_stage` 
FOR EACH ROW 
BEGIN

   DECLARE alumno INT;
   DECLARE profesor INT;
   DECLARE dto decimal(10,2);
   SET alumno = (SELECT user_id from `order` where `order`.id = NEW.order_id);
   SET profesor = (SELECT id_profesor from user where user.id = alumno);
   SET dto = (SELECT descuento from descuentos
   join user on descuentos.id_profesor = user.id
   join producto on producto.familia=descuentos.familia
   where producto.id = NEW.product_id and user.id = profesor);

   SET NEW.descuento = (SELECT CAST((((dto)*(NEW.pvp))/100) AS DECIMAL(10,2)));
END;
$$
DELIMITER ;

Read More Here 在这里阅读更多

Note this syntaxs but may not 'work' 请注意此语法,但可能无法正常工作

drop trigger if exists discount2;

DELIMITER $$
CREATE TRIGGER `discount2` 
BEFORE 
INSERT ON `order_stage` 
FOR EACH ROW 
begin
DECLARE alumno INT;
DECLARE profesor INT;
DECLARE dto decimal(10,2);
SET alumno = (SELECT user_id from `order` where `order`.id = NEW.order_id);
SET profesor = (SELECT id_profesor from user where user.id = alumno);
SET dto = (SELECT descuento from descuentos
join user on descuentos.id_profesor = user.id
join producto on producto.familia=descuentos.familia
where producto.id = NEW.product_id and user.id = profesor);

SET NEW.descuento = (SELECT CAST((((dto)*(NEW.pvp))/100) AS DECIMAL(10,2)));
end $$
DELIMITER ;

暂无
暂无

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

相关问题 #1064-您的SQL语法有误; 查看与您的MySQL服务器版本相对应的手册以获取正确的语法 - #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax #1064-您的SQL语法有误; 查看与您的MySQL服务器版本相对应的手册以获取正确的语法 - #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax 错误#1064 - 您的SQL语法出错; 查看与MySQL服务器版本对应的手册 - Error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ERROR 1064 (42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册 - ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version #1064 - 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册 - #1064 -You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version 1064 - 您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册 - 1064 - You have an error in your SQL syntax;check the manual that corresponds to your MySQL server version #1064-您的SQL语法有误; 检查与您的MySQL服务器版本相对应的手册 - #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version SQL错误(1064):您的SQL语法有错误; 查看与您的MySQL服务器版本相对应的手册以获取正确的语法 - SQL Error (1064) : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax MySQL ERROR 1064 (42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册 - MySQL ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ERROR 1064 (42000):您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册以了解要使用的正确语法 - ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM