[英]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
. 如果要在触发器中执行多个语句 ,则应使用
BEGIN
和END
。
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 ;
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.