再次认识mysql(七)存储过程

存储过程

  将若干条sql封装起来执行 将此过程存储在数据库中  

  创建语法  create procedure procedureName()

  查看已经存在的   show procedure status \G

  调用存储过程  call procedure() 

  存储过程是可以编程的  可以使用变量  表达式 控制结构 完成复杂的功能

 create procedure p1()

 begin

 select  'hello'  from dual;

 end$


 create procedure p2()

 begin 

      declare age int default 90

      declare height int default 175                               

      select concat ('年龄',age,'身高',height) from dual;

      end$

在存储过程中用declare 声明变量   

运算结果 可以赋值给变量     set 变量名: =表达式的值

if else控制结构   

 if   condition  then     statement

 else    

 end


create procedure p4()

begin 

   declare age int default 18;

   if age>=18 then

   select '已成年';

   else

   select '未成年';

   end if;

 end$ 


存储过程中可以传递参数    存储过程的括号中可以传递参数 

传递参数 有三种   in out  inout

in 输入型   作为输入型 参数

out 输出型   自定义变量接收 结果

inout输入输出型  输入型参数   结果仍为该参数


create procedure p5(width int,height int)

begin

 select concat('你的面积是',width*height) as area;

  if width>height then

  select  '很宽';

  elseif  width<height then

  select '很窄';

  else

  select '很方'

  end if;

end$

  call p5(4,6)$

计算1到100的和

 create procedure p6()

 begin

   declare total int default 0;

   declare num int default 0;

   while num<100 do

        set num :=num+1;

        set total :=total+num;

   end while;

   select total;

end$ 


create procedure p8(int n int,out total int)

begin 

    declare num int default 0;

     while num<n do

     set num:=num+1;

     set total:=total+num;

     end while;

end$


case 示例  

create procedure p10()

begin 

   declare pos  int default 0;

   pos =floor(5*rand());

   case  pos

   when 1 then select ‘still flying’;

   when 2 then select 'fall in sea';

   when 3 then select 'in the island';

   else select 'i do not  know';

   end case;

end$


repeat示例

  create procedure p1()

  begin 

      declare total int default 0;

      declare i int default 0;

      repeat 

      set i :=i+1;

      set total :=total+i;

      until i>=100 end repeat;

      select total;

  end$


cursor  游标  游动的标志  一条select结果集  取出资源的接口/句柄  沿着游标 可以一次取出一行

declare  游标名称  cursor for select _statement

open 游标名称   

fetch  游标名称  into var1,var2[...]

close 游标名称   


create procedure p1()

begin 

declare row_gid int;

declare rot_num int,;

declare row_name varchar(20);

declare cnt int default 0;

declare i int default 0;

declare getgoods  cursor  for   select gid,num,name from goods;

select count(*) into cnt from goods;

open getgoods;

repeat 

set i:=i+1;

fetch getgoods into row_gid,row_num,row_name;

select row_num,row_name;

until i>=cnt end repeat;

close getgoodsl; 

end$

在mysql中 可以使用declare continue  handler 来操作1个越界标识

exit与continue的区别是  exit 触发以后  后面的语句不会再执行

undo是触发以后,前面的语句撤销(目前mysql还不支持undo)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值