mysql存储过程

什么是存储过程

存储过程(Stored Procedure)是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象

存储过程就是具有名字的一段代码,用来完成一个特定的功能。
创建的存储过程保存在数据库的数据字典中

存储过程优点:


存储过程可封装,并隐藏复杂的商业逻辑。
存储过程可以回传值,并可以接受参数。
存储过程无法使用 SELECT 指令来运行,因为它是子程序,与查看表,数据表或用户定义函数不同。
存储过程可以用在数据检验,强制实行商业逻辑等。


缺点:


存储过程,往往定制化于特定的数据库上,因为支持的编程语言不同。当切换到其他厂商的数据库系统时,需要重写原有的存储过程。
存储过程的性能调校与撰写,受限于各种数据库系统。

存储过程的参数 :
参数类型:


IN输入参数:表示调用者向过程传入值(传入值可以是字面量或变量)
OUT输出参数:表示过程向调用者传出值(可以返回多个值)(传出值只能是变量)
INOUT输入输出参数:既表示调用者向过程传入值,又表示过程向调用者传出值(值只能是变量)

 

语法

 

delimiter//                            声明语句结束符,可以自定义(默认分号)
create procedure 名称(声明参数1,...)                声明存储过程
begin...end                            存储过程开始和结束符号
declare 变量名 变量类型 (unsigned default 默认值)        定义变量
set 变量名=变量值                        变量赋值
参数类型(in|out|inout) 参数名 数据类型(int、varchar等)    声明参数
 

例子:

delimiter //
drop procedure if exists orginfo;
create procedure orginfo(in Id int)
begin
declare orgTypes VARCHAR(6);
select orgType into orgTypes from orgbaseinfo 
where orgId=Id;
if orgTypes='01' then
select areainfo.* from orgbaseinfo inner join customerareamapinfo on orgbaseinfo.orgId=customerareamapinfo.customerID and customerareamapinfo.custType=2 and bindType=01
inner join areainfo on customerareamapinfo.areaID=areainfo.areaID
where orgbaseinfo.orgId=Id;
else
select areainfo.*  from orgbaseinfo inner join orgbaseorgmapinfo on orgbaseinfo.orgId=orgbaseorgmapinfo.mapOrgbaseId
inner join customerareamapinfo on orgbaseorgmapinfo.orgbaseId=customerareamapinfo.customerID and customerareamapinfo.custType=2 and bindType=01
inner join areainfo on customerareamapinfo.areaID=areainfo.areaID
where orgbaseinfo.orgId=Id;
end if;
end;
//
delimiter ;


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值