数据库学习笔记——使用存储过程

  • 使用存储过程:为以后的使用而保存mysql语句的集合

  • 为什么要使用存储过程

    • 通过把处理封装在容易使用的单元中,简化复杂的操作过程
    • 优惠不要求反复建立一些列处理步骤,这保证了数据的完整性
    • 简化变动的管理
    • 提高性能
    • 存在一些职能用在单个请求中的mysql元素和特性,存储过程中可以使用它们来编写功能更强更灵活的代码
  • 存储过程的缺陷

    • 一般来说存储过程编写笔记本sql语句复杂,编写存储过程需要更高的技能,更丰富的经验
    • 你可能没有创建储存过程的安全访问权限

备注:mysql命令行语句分隔符默认是分号;如果要更改命令行的语句分隔符 delimiter

delimiter //
将命令行语句分隔符改为//
  • 使用存储过程
    • 创建存储过程create procedure 存储过程名(in 输入参数 数据类型,out 输出参数 数据类型 ) begin select查询语句 into 输出参数 end (参数可以省略,输入参数用in,输出参数用out)
create procedure 存储过程名(in 输入参数 数据类型,out 输出参数 数据类型 )
 begin
 select查询语句 into 输入参数
 end
 (参数可以省略,输入参数用in,输出参数用out)
delimiter //
create procedure ordertotal(in onumber int,out ototal decimal(10.2)
begin
select sum(item_price * quantity) from orderitems where order_num=onumber into ototal
end //
- 使用存储过程`call 存储过程名(输入参数,@输出参数)`
delimiter ;
call ordertatal(20005,@total)
delimiter //
create procedure ordertotal (in onumber int,in taxalbe boolean,out ototal decimal(10,2))
begin 
declare total  decimal(10,2);
declare taxrate int default 6;
select sum(item_price * quantity) from orderitems where order_num=onumber into total;
if taxable then
select total *(1+taxrate/100) into total;
end if;
select total into ototal;
end //
call ordertotal(20005,0,@otoral)//
select @ototal
首次将命令语句分隔符改成//
创建存储过程名称为ordertotal,输入参数为onumber 整型,taxalbe 布尔型 输出参数ototal 数字类型(小数点左右最大共10位,小数点后两位小数)
begin 和end之间写select语句:声明一个变量total 数字类型(10,2),又声明一个变量taxrate 整型,默认值为6
把产品订单的合计赋值给total中,如果taxalbe为真那么计算含税值赋给total
执行存储过程:输入参数:20005(订单编号),布尔值为0(不需要交营业税),输出参数ototal
查询:ototal值
  • 检查存储过程show create procedure 存储过程名
  • 删除存储过程drop procedure 存储过程名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值