[英]mysql procedure error in IF..THEN statement
So I have this procedure to calculate freights. 所以我有这个程序来计算运费。 I need to select two values from matching row. 我需要从匹配的行中选择两个值。 And according to condition based on in_quantity
, out of these two previously selected values one will be set to out_total
. 并且根据基于in_quantity
条件,将在这两个先前选择的值中将一个设置为out_total
。
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
out_total = val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2;
END IF;
END$$
DELIMITER ;
When executed it gives following 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 '= val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2' at line 16
beacause any value assign to a variable in mysql , we use SET
keyword. 因为任何赋值给mysql中变量的值,我们都使用SET
关键字。 so change only in procedure 所以只改变程序
SET out_total = val1;
SET out_total = val1;
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
SET out_total = val1;
END IF;
IF(in_quantity > 9) THEN
SET out_total = val1;
END IF;
END$$
DELIMITER ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.