前言

这里存储过程和游标的定义和作用就不介绍了,网上挺多的,只通过简单的介绍,然后用个案例让大家快速了解。实例中会具体说明变量的定义,赋值,游标的使用,控制语句,循环语句的介绍。

1.创建存储过程。

create procedure myproc(out s int)
    begin
      select count(*) into s from students;
    end

存储过程根据需要可能会有输入、输出、输入输出参数,如果有多个参数用”,”分割开。mysql存储过程的参数用在存储过程的定义,共有三种参数类型,in,out,inout:

  • in参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回,为默认值
  • out:该值可在存储过程内部被改变,并可返回
  • inout:调用时指定,并且可被改变和返回

2.查看存储过程名称

select routine_name from information_schema.routines where routine_schema='数据库名称';

3.调用存储过程

call myproc()

4.删除存储过程

drop procedure if exists myproc;

因为mysql中游标只能在存储过程和方法中使用,所以就直接通过案例介绍游标。

案例:该案例采用无参存储过程,有参的也挺简单,根据上面的介绍,对应实现就行,该存储过程主要就创建一个存储过程,用它做查询 修改等操作。

#检查该存储过程是否存在 存在就删除了再创建
drop procedure if exists processnames ;
#创建存储过程
create procedure processnames()
#begin end  存储过程中的sql逻辑写在begin 和end 中
begin
#定义变量
declare var_name varchar(300);
declare var_uuid varchar(300);
declare count int default 0 ;
declare i int default 0 ;
declare done int;
#定义游标
declare namecursor cursor for select parentid from datadictionary group by parentid;
#该sql语句作用是  在你遍历游标的时候  游标循环结束 就会执行这句话,并给done赋值为1 然后循环就会停止 但该语句不适用while 循环语句。
declare continue handler for sqlstate '02000' set done = 1;
#查询一个表分组的总数量  并赋值给count变量
select count(*) into count from (select parentid from datadictionary group by parentid) t1;
#打开游标
open namecursor;
#循环体
    while i<count do
    #拿到游标指向的当前行的数据  并赋值给var_name变量
    fetch namecursor into var_name;
    #通过var_name变量进行相应的数据查询 把查询出来的数据通过into赋值给变量var_uuid
    select uuid into var_uuid from datadictionary where id=var_name;
    #进行修改操作
    update datadictionary set parentid=var_uuid where parentid=var_name;
    #可以通过set 给变量赋值  这里用来记录循环体执行了多少次  看和游标遍历的条数是否一致
  set i=i+1;
    #当i>count的时候循环体结束
  end while;
    #关闭游标
    close namecursor;
    #打印i 
    select i;
    #查看修改后的数据
    select * from datadictionary;
    #存储过程结束标志
end;
#调用存储过程
call processnames()

案例2 添加操作,并且获取游标中的多列值

drop procedure if exists proce;
create procedure proce()
begin
 declare  userid varchar(50);
 declare  depatementid varchar(50);
 declare  done int; 
 declare namecuursor cursor for select id,frameworkdepartmentid from frameworkusers ;
 declare  continue handler for sqlstate '02000' set done = 1;
 open namecuursor;
label: loop
  fetch namecuursor into userid,depatementid;
    if done = 1 then
        leave label; 
    end if; 
    insert into frameworkdepartmentuser(id,frameworkuserid,frameworkdepartmentid) values(uuid(),userid,depatementid);
end loop label;
close namecuursor;
end;
call proce();
select * from frameworkdepartmentuser

注意:

1.declare关键字 在存储过程内部就是定义变量的。

2.declare namecursor cursor for select parentid from datadictionary group by parentid;该语句用来定义游标,其中for后面跟随的是你的查询语句,把查询出来的结果赋值给了namecursor这个游标

2.1 遍历游标前一定要先打开游标 open namecursor;

2.2 遍历结束 一定要关闭游标,close namecursor;如果不关闭当存储过程执行完遇到最后一个end的时候也会自动关闭。

3.declare continue handler for sqlstate ‘02000’ set done = 1; 有很多人不知道这段是什么意思,我开始也不知道,首先本案例中这段话可以直接注释,没有作用,一般在没有条件体的循环体中使用,如:repeat statement_list until search_condition end repeat;循环体和loop 循环体使用的比较多。该句话的意思如注释所说,当你没有循环条件作为循环结束语句的时候,就可以用到上面declare continue handler for sqlstate ‘02000’ set done = 1;该句话的作用就是:当你游标在上述循环体中遍历结束的时候,就会执行该语句,让done =1;打个比方,假如你用的是repeat statement_list until search_condition end repeat;语句作为循环体,我们可写成until done因为repeat循环体执行顺序是先执行一次sql语句流然后再进行until done进行判断,当游标遍历结束,done=1然后循环体结束。sqlstate ‘02000’只是一个标识,具体可以官网查询。

然后再简单说下

label: loop
    statement_list

    if done=1 then
        leave label; 
    end if; 
end loop label;

其中statement_list还是你需要执行的sql流,loop和repeat有点像,都是先执行,再判断。我案例中是用的while 该循环体是先判断条件,再执行,比较符合我当前的运用场景。

4. fetch namecursor into var_name;把当前游标指向的行赋值给变量,这里游标执行过程是顺序执行的,就相当于遍历数组,从第一个一直遍历到最后一个。

5. 如果需要把查询出来的结果,赋值给一个变量,可以使用into如这样:select uuid into var_uuid from datadictionary where id=var_name;

6. 如果非查询赋值,可以用set如:set i=i+1;

7. 获取一行游标中的多列值,只需定义和列关联的变量,然后用into进行赋值 如例2:fetch namecuursor into userid,depatementid;

总结

到此这篇关于mysql存储过程和游标使用的文章就介绍到这了,更多相关mysql存储过程游标使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!