在MySQL中正确调用存储过程

时间:2016-01-19 11:57:34

标签: mysql stored-procedures

这是我的存储过程:

DELIMITER //
CREATE PROCEDURE facturar(codigo int(6),fecha date,cedula VARCHAR(9),cantidad int(3),peso VARCHAR(10),precio varchar(10))
BEGIN
DECLARE P float;
Declare total float;
set P=0;
set total=0;
insert into factura VALUES(ID,fecha,cedula,cantidad, peso, `precio`);
set P=peso*4;
if P between 2000 and 10000 then SET precio= (P*0.05)+(P*0.12)+P;
elseif P > 10000 then set precio= (P*0.05)+(P*0.10)+(P*0.12)+P;
else set precio= (P*0.12)+P;
end if;
set total = precio;
select  concat("Total de envio=", total);
END;
//

当我拨打电话时:

call facturar (NULL,'2016/01/13', 'v18834415', '2', '96400', '');

显示结果:

Total de envió=489712

但是,一旦我查看表格,“precio”是白色的,而其他值则被填充。

2 个答案:

答案 0 :(得分:1)

您将precio设置为insert中的列。然后你做了一些计算,从不对这些值做任何事情。

我强烈鼓励两件事:

  • 通过使用变量前缀
  • 指示变量和列之间的差异
  • 列出insert
  • 的所有列

在您的情况下,我认为您需要在设置precio

后移动插入内容
CREATE PROCEDURE facturar(
    in_codigo int(6),
    in_fecha date,
    in_cedula VARCHAR(9),
    in_cantidad int(3),
    in_peso VARCHAR(10),
    in_precio varchar(10)
)
BEGIN
    DECLARE @P float;
    Declare @total float;
    set @P = 0;
    set @total = 0;
    set @P = in_peso*4;
    if @P between 2000 and 10000 then SET in_precio= (@P*0.05)+(@P*0.12)+@P;
    elseif @P > 10000 then set in_precio= (@P*0.05)+(@P*0.10)+(@P*0.12)+@P;
    else set in_precio = (@P*0.12)+@P;
    end if;
    set @total = in_precio;
    insert into factura( . . . )
        VALUES(in_ID, in_fecha, in_cedula, in_cantidad, in_peso, in_precio);
    select concat('Total de envio=', @total);
END;

答案 1 :(得分:0)

您需要在设置值后执行插入查询。

DELIMITER //
CREATE PROCEDURE facturar(codigo int(6),fecha date,cedula VARCHAR(9),cantidad int(3),peso VARCHAR(10),precio varchar(10))
BEGIN
DECLARE P float;
Declare total float;
set P=0;
set total=0;

set P=peso*4;
if P between 2000 and 10000 then SET precio= (P*0.05)+(P*0.12)+P;
elseif P > 10000 then set precio= (P*0.05)+(P*0.10)+(P*0.12)+P;
else set precio= (P*0.12)+P;
end if;
set total = precio;
select  concat("Total de envio=", total);
insert into factura VALUES(ID,fecha,cedula,cantidad, peso, total);
END;
//

在插入查询中使用总计变量而不是反引用值