如何在mysql中使用diffdate函数使用变量

时间:2013-04-25 15:43:12

标签: mysql variables

CREATE DEFINER=`root`@`localhost` PROCEDURE `bill`(subid int(10), uprice float, sd date, ed date)
BEGIN
declare numberofdays int;
declare totalprice float;

set numberofdays = datediff(ed,sd);
set totalprice= numberofdays*uprice;

insert into invoice (subscriptionid,price_perunit,numberofdays,totalprice) 
values (subid, uprice,numberofdays,totalprice);

1 个答案:

答案 0 :(得分:0)

您的代码(语法)是正确的。您可能对变量和字段名称有疑问,请尝试使用不同的名称,例如 -

CREATE DEFINER = root@localhost PROCEDURE bill (subid int(10), uprice float, sd date, ed date)
BEGIN
  DECLARE var_numberofdays int;
  DECLARE var_totalprice float;

  SET var_numberofdays = DATEDIFF(ed, sd);
  SET var_totalprice = var_numberofdays * uprice;

  INSERT INTO invoice (subscriptionid, price_perunit, numberofdays, totalprice)
    VALUES (subid, uprice, var_numberofdays, var_totalprice);

  ...
END;