我制作了一个SP,尝试按有效日期顺序删除项目,假设不同批次的有效日期不同,我需要出售这些产品,我需要出售更接近到期日期的产品,但是我不明白为什么它不会让我添加此SP。

我的SP获得3个参数:id_nombrecantfecha,其中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,然后从下一个更接近到期。最后,完成该操作后,需要将该销售添加到销售表(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

关于mysql - 为什么这个MySQL存储过程不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30225305/

10-13 01:21