代码片段 mysql必知必会 存储过程 chapter 23


-- show tables;


-- code demonstarte the learning of procedure
-- example ch23.3.2 try to simple procedure
delimiter //
create procedure productpricing1()
begin
	select avg(prod_price)as priceaverage
	from products;
end //
delimiter ;

call productpricing1();

drop procedure productpricing1;


-- example ch23.3.4 procedure with parameter

delimiter //
create procedure productpricing(
out pl decimal(8,2),
out ph decimal(8,2),
OUT PA decimal(8,2)
)
begin
	select min(prod_price)
	into pl
	from products;
	
	select max(prod_price)
	into ph
	from products;

	select avg(prod_price)
	into pa
	from products;

end // -- 容易错
delimiter ;

call productpricing(@plowprice,@pHigh,@pAverage);
select @pAverage,@plowprice,@pHigh;



-- final ch23.3.5 最终作业

delimiter //
create procedure ordertotal(
in onumber int,
in taxable boolean,
out ototal decimal(8,2)
)comment 'get order total'
begin
declare total decimal(8,2);
	declare taxrate int default 6;
	
	select sum(item_price*quantity)
	from orderitems
	where order_num = onumber
	into total;

	-- is this taxable
	if taxable then
		select total +(total/100*taxrate) 
		into total;
	end if;
	-- finally
	select total into ototal;

end //

delimiter ;

-- call main
call ordertotal(20005,0,@total);
select @total;

call ordertotal(20005,1,@total);
select @total;

show create procedure ordertotal;
drop procedure ordertotal;














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值