概念:类似函数,把一段代码封装起来,当需要执行这一段代码的时候,可以通过调用该存储过程来实现。
在封装的语句体里面,可以用if/else,case,while等控制结构
可以进行sql编程。
与函数的区别:
1、名称不同function和procedure
2、存储过程没有返回值,函数需要在末尾添加return
查看当前已存在的存储过程:
show procedure status;
show procedure status \G
创建存储过程:
delimiter $ //修改sql语句的结尾符合
create procedure p1()
begin
select * from g1;
end$
查看存储过程的创建语句:
show create procedure p1 \G
调用创建的存储过程:
call p1(); 小括号里面可以传递参数
删除存储过程:
drop procedure p1;
create procedure p2(n int)
begin
select * from g where num > n;
end$
call p(10);
create procedure p3(n int,j char(1))
begin
if j = 'h' then
select * from g where num > n;
else
select * from g where num < n;
end if;
end$
call p3(10,'h')$
计算1-->n的和
create procedure p4(n smallint)
begin
declare i int;
declare s int;
set i=1;
set s=0;
while i <=n do
set s = s + i;
set i = i + 1;
end while;
select s;
end$
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24103417/viewspace-1415101/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/24103417/viewspace-1415101/