为什么这个 MySQL 存储过程不起作用?
Why won't this MySQL stored procedure work?
我制作了一个 SP,它试图按照衰老日期的顺序移除物品,假设不同批次有不同的衰老日期,它就可以工作,我需要卖掉那些接近过期的,但我不明白为什么它不会让我添加这个 SP。
我的 SP 有 3 个参数:id_nombre
、cant
和 fecha
其中 id_nombre
是产品的 ID,cant
是数量我想出售,fecha
是出售日期。我的SP是这样的:
DELIMITER $$
BEGIN
DECLARE cantidad_previa int; /*I use this var to store the total of products before the sell.*/
DECLARE lote int; /*stores the id of the batch that is closer to expire*/
DECLARE aux_cont int; /*its a counter that stores the ramaining products to sell*/
DECLARE cant_lote int; /*stores how many products has a batch.*/
SET aux_cont = cant;
select cantidad into cantidad_previa from productos where id_producto=id_nombre;
if cantidad_previa>cant
then
WHILE aux_cont>0 do
select id_lote into lote FROM lotes where id_producto=id_nombre and cantidad>0 order by fecha_exp limit 1;
select cantidad into cant_lote from lotes where id_lote=lote;
if cant_lote>aux_cont
then
set cant_lote = cant_lote - aux_cont;
update lotes set cantidad = cant_lote where id_lote = lote;
set aux_cont = 0;
else
update lotes set cantidad = 0 where id_lote=lote;
set aux_cont = aux_cont - cant_lote;
end if;
end while;
INSERT INTO registro_ventas VALUES (NULL, id_nombre, cant, fecha);
else
return
end if;
END $$
DELIMITER ;
我想做的是验证产品总数是否大于询问产品的数量。然后,我继续验证要求的数量是否小于确定批次的数量,如果是这样我就从该批次中减去它们,如果不是我需要将该批次置为 0 并从更接近的下一个开始到期。最后,完成后我需要将该销售额添加到销售额 table (registro_ventas).
编辑:我可以这样做
DELIMITER $$
CREATE PROCEDURE `registrarventa` (id_nombre INT, cant INT, fecha DATE)
BEGIN
DECLARE cantidad_previa int;
DECLARE lote int;
DECLARE aux_cont int;
DECLARE cant_lote int;
SET aux_cont = cant;
select cantidad into cantidad_previa from productos where id_producto=id_nombre;
if cantidad_previa>cant
then
WHILE aux_cont>0 do
select id_lote into lote FROM lotes where id_producto=id_nombre and cantidad>0 order by fecha_exp limit 1;
select cantidad into cant_lote from lotes where id_lote=lote;
if cant_lote>aux_cont
then
set cant_lote = cant_lote - aux_cont;
update lotes set cantidad = cant_lote where id_lote = lote;
set aux_cont = 0;
else
update lotes set cantidad = 0 where id_lote=lote;
set aux_cont = aux_cont - cant_lote;
end if;
end while;
INSERT INTO registro_ventas VALUES (NULL, id_nombre, cant, fecha);
end if;
END$$
DELIMITER ;
您缺少语法
CREATE PROCEDURE simpleproc (IN para1 varcahr(255), OUT param2 INT)
此语句需要在 BEGIN
之前和 Delimiter
之后。
有关创建存储过程的更多信息,请参阅 Here
我制作了一个 SP,它试图按照衰老日期的顺序移除物品,假设不同批次有不同的衰老日期,它就可以工作,我需要卖掉那些接近过期的,但我不明白为什么它不会让我添加这个 SP。
我的 SP 有 3 个参数:id_nombre
、cant
和 fecha
其中 id_nombre
是产品的 ID,cant
是数量我想出售,fecha
是出售日期。我的SP是这样的:
DELIMITER $$
BEGIN
DECLARE cantidad_previa int; /*I use this var to store the total of products before the sell.*/
DECLARE lote int; /*stores the id of the batch that is closer to expire*/
DECLARE aux_cont int; /*its a counter that stores the ramaining products to sell*/
DECLARE cant_lote int; /*stores how many products has a batch.*/
SET aux_cont = cant;
select cantidad into cantidad_previa from productos where id_producto=id_nombre;
if cantidad_previa>cant
then
WHILE aux_cont>0 do
select id_lote into lote FROM lotes where id_producto=id_nombre and cantidad>0 order by fecha_exp limit 1;
select cantidad into cant_lote from lotes where id_lote=lote;
if cant_lote>aux_cont
then
set cant_lote = cant_lote - aux_cont;
update lotes set cantidad = cant_lote where id_lote = lote;
set aux_cont = 0;
else
update lotes set cantidad = 0 where id_lote=lote;
set aux_cont = aux_cont - cant_lote;
end if;
end while;
INSERT INTO registro_ventas VALUES (NULL, id_nombre, cant, fecha);
else
return
end if;
END $$
DELIMITER ;
我想做的是验证产品总数是否大于询问产品的数量。然后,我继续验证要求的数量是否小于确定批次的数量,如果是这样我就从该批次中减去它们,如果不是我需要将该批次置为 0 并从更接近的下一个开始到期。最后,完成后我需要将该销售额添加到销售额 table (registro_ventas).
编辑:我可以这样做
DELIMITER $$
CREATE PROCEDURE `registrarventa` (id_nombre INT, cant INT, fecha DATE)
BEGIN
DECLARE cantidad_previa int;
DECLARE lote int;
DECLARE aux_cont int;
DECLARE cant_lote int;
SET aux_cont = cant;
select cantidad into cantidad_previa from productos where id_producto=id_nombre;
if cantidad_previa>cant
then
WHILE aux_cont>0 do
select id_lote into lote FROM lotes where id_producto=id_nombre and cantidad>0 order by fecha_exp limit 1;
select cantidad into cant_lote from lotes where id_lote=lote;
if cant_lote>aux_cont
then
set cant_lote = cant_lote - aux_cont;
update lotes set cantidad = cant_lote where id_lote = lote;
set aux_cont = 0;
else
update lotes set cantidad = 0 where id_lote=lote;
set aux_cont = aux_cont - cant_lote;
end if;
end while;
INSERT INTO registro_ventas VALUES (NULL, id_nombre, cant, fecha);
end if;
END$$
DELIMITER ;
您缺少语法
CREATE PROCEDURE simpleproc (IN para1 varcahr(255), OUT param2 INT)
此语句需要在 BEGIN
之前和 Delimiter
之后。
有关创建存储过程的更多信息,请参阅 Here