写一个mysql的存储过程

--procedure name: ordertotal

--parameters: onnumber = order number

--      taxable = 0 if not taxable, 1 if taxable

--      ototal = order total variable

CREATE PROCEDURE ordertotal (

  in onnumber int,

  in taxable boolean,

  out ototal decimal(8,2)

) comment 'obtain order total, optionally adding tax'

BEGIN

  --先声明变量

  declare total decimal(8,2);

  declare taxrate int default 6;

  --计算

  select sum(item_price*quantity)

  from orderitems

  where order_num = onnumber

  into total;

  --是否需要纳税?由你传的参数决定

  if taxable then

    select total + (total/100*taxrate) into total;

  end if;--必须if和end if配对,也可以用elseif,顺序执行

  --最后当然把结果赋给out的ototal

  select total into ototal;

END;

 

--要试验这个存储过程,要用如下语句

call ordertotal(2001,1,@total);

select @tatal;

--备注:1、显示创建一个存储过程的create语句,使用show create procedure ordertotal;

--2、为了获得何时、由谁创建等详细信息的存储过程列表,使用show procedure status,当然,这条语句你还可以加like进行过滤,如

--show procedure status like 'ordertotal';

--3、这个过程比较简单,但很多方面都介绍到了。mark.

 

posted @ 2016-08-26 09:07  鲁山人  阅读(223)  评论(0)    收藏  举报