过程调用函数与表字段

时间:2015-06-09 04:10:39

标签: mysql function stored-procedures

如何使用表字段调用过程中的函数?

我已经阅读了问题How to call a stored procedure using table data,但情况有所不同。

我有一个功能:

CREATE FUNCTION valorPedido (qtde int, decimal value (11.2)) RETURNS double
BEGIN
 RETURN qtde * value;
END

我有程序ItemPedido:

CREATE PROCEDURE ItemPedido (numped int, int codtab, codpro int (4), qtdped int, decimal valitem (11.2), decimal valped (11.2), datped date)
BEGIN
insert into itens_pedido (numped, codtab, codpro, qtdped, valitem, valped, datped) values ​​(numped, codtab, codpro, qtdped, valitem, valped, datped);
END

一张名为itens_Pedido的表:

IdItemPedido int (11) AI PK
NumPed int (11)
CodTab int (11)
CodPro int (4)
QtdPed int (11)
ValItem decimal (11.2)
ValPed decimal (11.2)
DatPed date

我想以这种方式调用程序:

call ItemPedido (1, 99, 1, 10, 4, valorPedido (qtdped, valitem), sysdate());

错误代码:1054。未知栏' qtdped'在'字段列表'

但我只能这样:

call ItemPedido (1, 99, 1, 10, 4, valorPedido (10, 4), sysdate());

1 个答案:

答案 0 :(得分:0)

你可以做这样的事情;

从sp中删除valped param并使用函数计算在插入行后更新表。

CREATE PROCEDURE ItemPedido (numped int, int codtab, codpro int (4), qtdped int, decimal valitem (11.2),  datped date)
    BEGIN

    insert into itens_pedido (numped, codtab, codpro, qtdped, valitem, datped) 
values ​​(numped, codtab, codpro, qtdped, valitem, datped);

set @lastId=LAST_INSERT_ID();

    set @valped= (select valorPedido(qtdped,valitem) from  itens_pedido where AI =@lastId);

update itens_pedido set valped=@valped where AI=@lastId;

    END