MySQL的存储过程
是一组为了完成特定功能的MySQL语句集,存储过程是由轮流控制和MySQL语句书写的存储过程,这个过程经编译和优化后存储在数据库服务器中,存储过程可由应用程序通过一个调用来执行,而且允许用户声明变量。同时,存储过程可以接收和输出参数、返回执行存储过程的状态值,也可以嵌套和调用。
为什么要使用存储过程
存储过程是已经被认证的技术!
存储过程会使系统运行的更快!
存储过程是可以重复使用的组件!他是数据库逻辑而不是应用程序。
存储过程将被保存!
存储过程的优点
存储过程只是在创造时进行编译,以后每次执行存储过程都不需要在重新编译,而一般 的MySQL语言每执行一次就需要重,新编译一次,所以使用存储过程可提高数据库执行速度。
当对数据进行复杂操作时(如对多个表进行Update,Insert,Query,Delete时),可 以将此复杂 的操作用存储过程封装起来与数据库提供的事务处理结合在一起使用。
存储过程可以重复使用,可以减少数据库开发人员的工作量。
安全性高,可以设定只有某此用户才具有对指定存储过程的使用权。
存储过程的作用:
第一:存储过程因为baiSQL语句已经预编绎过了,因此运行的速度比较快zhi。
第二:存储过程可接受参数、输出参数、返回单个或多个结果集及返回值。向程序返回错误原因。
第三:存储过程运行比较稳定,不会有太多的错误。只要一次成功,以后都会按这个程序运行。第四:存储过程主要是在服务器上运行,减少对客户机的压力。
第五:存储过程可以包含程序流、逻辑以及对数据库的查询。同时可以实体封装和隐藏数据逻辑。第六:存储过程可以在单个存储过程中执行一系列SQL语句。
第七:存储过程可以从自己的存储过程内引用其它存储过程,这可以简化一系列复杂语句。
1-创建一个最简单的存储过程
create procedure proc_生产制造部()
begin
select * from 员工 where 部门=‘生产制造部’;
end
调用
call proc_生产制造部()
2-如何在存储过程中定义变量,并给变量赋值
create procedure proc01()
begin
declare i int;
set i=10;
select i as result;
end
调用
call proc01()
3-如何给存储过程定义参数
drop procedure if exists proc02;
create procedure proc02(in num int)
begin
declare num02 int;
declare num03 int;
set num02=10;
set num03 = num+num02;
select num03 as result;
end
调用
call proc02(30)
drop procedure if exists proc03;
create procedure proc03(in a int,in b int)
begin
declare result int;
set result = a+b;
select result;
end
call proc03(1,2)
create procedure proc_depart_info(in departName varchar(255))
begin
select * from 员工 where 部门=departName;
end
call proc_depart_info(‘管理部’)
4-简单的条件控制语句
create procedure proc04(in x int)
begin
if x=1 then
select 1;
elseif x=2 then
select 2;
else
select 3;
end if;
end;
call proc04(4)
create procedure proc04(in x int)
begin
if x=1 then
select 1;
elseif x=2 then
select 2;
else
select 3;
end if;
end;
create procedure proc05(in x int)
begin
case x
when 1 then select 1;
when 2 then select 2;
else
select 3;
end case;
end
call proc05(4)
create procedure proc06(in x int)
begin
case x
when 1 then select 1;
when 2 then select 2;
else
select 3;
end case;
end
case
when value then …
[when value then…]
[else…]
end case
create procedure proc07(in x int)
begin
case
when x=1 then select 1;
when x=2 then select 2;
else
select 3;
end case;
end
call proc07(3)
带有输出 类型的参数示例
create procedure proc08(in a int,in b int,out c int)
begin
set c=a+b;
select c as result;
end
–declare @result int;
call proc08(1,2,@result);
select @result as 计算结果
结合示例:
1、查询1997年7月份客户采购的详细信息(客户 ,訂貨主檔 , 訂貨明細 )
select a.客户编号,a.公司名称,a.联系人,a.地址,b.订单号码,b.订单日期,b.收货人,
d.产品,e.类别名称,f.供应商,c.单价,c.数量,c.折扣 from 客户 a,订货主档 b,订货明细 c ,产品资料 d,产品类别 e ,商品供应 f where a.客户编号=b.客户编号 and b.订单号码=c.订单号码 and c.产品编号=d.产品编号 and d.类别编号=e.类别编号 and d.供应商编号=f.供应商编号
and 订单日期 between ‘1997-7-1’ and ‘1997-7-31’
drop procedure if exists proc_orderInfo;
create procedure proc_orderInfo()
begin
select a.客户编号,a.公司名称,a.联系人,a.地址,b.订单号码,b.订单日期,b.收货人,
d.产品,e.类别名称,f.供应商,c.单价,c.数量,c.折扣 from 客户 a,订货主档 b,订货明细 c ,产品资料 d,产品类别 e ,商品供应 f where a.客户编号=b.客户编号 and b.订单号码=c.订单号码 and c.产品编号=d.产品编号 and d.类别编号=e.类别编号 and d.供应商编号=f.供应商编号
and 订单日期 between ‘1997-7-1’ and ‘1997-7-31’;
end
call proc_orderInfo()
drop procedure if exists proc_orderInfo02;
create procedure proc_orderInfo02(in startDate date,in endDate date)
begin
select a.客户编号,a.公司名称,a.联系人,a.地址,b.订单号码,b.订单日期,b.收货人,
d.产品,e.类别名称,f.供应商,c.单价,c.数量,c.折扣 from 客户 a,订货主档 b,订货明细 c ,产品资料 d,产品类别 e ,商品供应 f where a.客户编号=b.客户编号 and b.订单号码=c.订单号码 and c.产品编号=d.产品编号 and d.类别编号=e.类别编号 and d.供应商编号=f.供应商编号
and 订单日期 between startDate and endDate;
end
call proc_orderInfo02(‘1997-01-01’,‘1997-12-31’)
drop procedure if exists proc_orderInfo03;
create procedure proc_orderInfo03(in _month int)
begin
select a.客户编号,a.公司名称,a.联系人,a.地址,b.订单号码,b.订单日期,b.收货人,
d.产品,e.类别名称,f.供应商,c.单价,c.数量,c.折扣 from 客户 a,订货主档 b,订货明细 c ,产品资料 d,产品类别 e ,商品供应 f where a.客户编号=b.客户编号 and b.订单号码=c.订单号码 and c.产品编号=d.产品编号 and d.类别编号=e.类别编号 and d.供应商编号=f.供应商编号
and month(订单日期)=_month;
end
call proc_orderInfo03(8)
create view view_orderInfo
as
select a.客户编号,a.公司名称,a.联系人,a.地址,b.订单号码,b.订单日期,b.收货人,
d.产品,e.类别名称,f.供应商,c.单价,c.数量,c.折扣 from 客户 a,订货主档 b,订货明细 c ,产品资料 d,产品类别 e ,商品供应 f where a.客户编号=b.客户编号 and b.订单号码=c.订单号码
and c.产品编号=d.产品编号 and d.类别编号=e.类别编号 and d.供应商编号=f.供应商编号
drop procedure if exists proc_orderInfo04;
create procedure proc_orderInfo04(in _month int,out customerId varchar(255))
begin
select * from view_orderInfo where month(订单日期)=_month;
select 客户编号 from view_orderInfo group by 客户编号 order by sum(单价数量(1-折扣)) desc limit 1 into customerId;
end
call proc_orderInfo04(7,@customerid);
select @customerid as 最高订单客户;
drop procedure if exists proc_orderInfo05;
create procedure proc_orderInfo05(in _month int,out customerId varchar(255))
begin
select * from view_orderInfo where month(订单日期)=_month;
set customerId =(select 客户编号 from view_orderInfo group by 客户编号 order by sum(单价数量(1-折扣)) desc limit 1);
end
call proc_orderInfo05(7,@customerid);
select @customerid as 最高订单客户;
select * from view_orderInfo where month(订单日期)=_month;
drop procedure if exists proc_orderInfo06;
create procedure proc_orderInfo06(in _month int,in type int,out result varchar(255))
begin
if type=1 then
set result =(select 客户编号 from view_orderInfo group by 客户编号 order by sum(单价数量(1-折扣)) desc limit 1);
else
set result =(select sum(单价数量(1-折扣)) from view_orderInfo group by 客户编号 order by sum(单价数量(1-折扣)) desc limit 1);
end if;
end
create procedure myproc()
begin
call proc_orderInfo06(7,1,@customerId);
call proc_orderInfo06(7,2,@money);
select @customerId as 最高订单客户,format(@money,2) 订单总金额;
end
call myproc()
DROP PROCEDURE IF EXISTS test;
CREATE PROCEDURE test(IN inparms INT, OUT outparams varchar(32))
BEGIN
DECLARE var CHAR(10);
IF inparms = 1 THEN
SET var = ‘hello’;
ELSE
调用存储过程
call test(2,@out);
select @out