mysql存储过程

#第1个存储过程,体会"封装sql"
delimiter $
drop procedure if exists p1;$ #删除存储过程
create procedure p1()#创建存储过程
begin
select * from rooms;
end;$
call p1();$#调用存储过程
#第2个存储过程,体会"参数"
delimiter $
drop procedure if exists p2;$
create procedure p2(n bigint(20))
begin
select * from rooms where id>n;
end;$
call p2(10050);$
#第3个存储过程,体会“控制结构”
delimiter $
drop procedure if exists p3;$
create procedure p3(n bigint(20),j char(1))
begin
if j='h' then #存储过程的If判断语句
select * from rooms where id>n;
else
select * from rooms where id<n;
end if;#这里必须要用end if结束
end;$
call p3(10050,'h');$
call p3(10050,'d');$
#第4个储存过程,体会"循环语句" (计算1-n的和)
delimiter $
drop procedure if exists p4;$
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,不能用+=
set i=i+1;
end while;#这里循环也必须要用end结束
select s;
end;$
call p4(200);$

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章