为什么这个MySQL存储过程不起作用?

时间:2015-05-13 21:13:44

标签: mysql stored-procedures

我已经制作了一个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 ;

1 个答案:

答案 0 :(得分:0)

您缺少语法

CREATE PROCEDURE simpleproc (IN para1 varcahr(255), OUT param2 INT)

此声明需要在BEGIN之前和Delimiter之后。

有关创建存储过程的详细信息,请参阅Here