MYSQL游标嵌套循环示例

来源:互联网 发布:衬衫品牌 知乎 编辑:程序博客网 时间:2024/04/30 02:24

本文将举例演示游标的嵌套用法,首先建造表结构,如下:

Sql代码  收藏代码
  1. drop table if exists `tb_user`;  
  2. create table tb_user (  
  3.     id bigint(22) not null auto_increment primary key,  
  4.     name varchar(20) not NULL,  
  5.     age  tinyint(3),  
  6.     gmt_create date,  
  7.     gmt_modified date  
  8. );  
  9.   
  10. drop table if exists `tb_user_param`;  
  11. create table tb_user_param (  
  12.     id bigint(22) not null auto_increment primary key,  
  13.     user_id bigint(22) not null,  
  14.     code varchar(100) not null,  
  15.     value varchar(1000),  
  16.     gmt_create DATE,  
  17.     gmt_modified date  
  18. );  
  19.   
  20. drop procedure if exists `sp_init_data`;  
  21. create procedure `sp_init_data`()  
  22. begin  
  23.     declare v_i bigint(22) default 0;  
  24.     declare v_uid bigint(22);  
  25.     declare v_nick varchar(20) default 'lanbo_';  
  26.     declare v_code_1 varchar(20) default 'address';  
  27.     declare v_code_2 varchar(20) default 'phone';  
  28.     declare v_code_3 varchar(20) default 'wangwang_id';  
  29.     declare v_value_1 varchar(20) default 'HZ.XiHu';  
  30.     declare v_value_2 varchar(20) default '1875757198';  
  31.     declare v_value_3 varchar(20) default 'shansun_';  
  32.       
  33.     while v_i < 10 do  
  34.         set v_i = v_i + 1;  
  35.         insert into tb_user values (null, concat(v_nick, v_i), 23, now(), now());   
  36.         select LAST_INSERT_ID() into v_uid;   
  37.         insert into tb_user_param values(null, v_uid, v_code_1, v_value_1, now(), now());  
  38.         insert into tb_user_param values(null, v_uid, v_code_2, concat(v_value_2, v_i), now(), now());  
  39.         insert into tb_user_param values(null, v_uid, v_code_3, concat(v_value_3, v_i), now(), now());  
  40.     end while;      
  41.     commit;  
  42. end;  
  43. call sp_init_data();  
  44.   
  45.   
  46. drop table if exists `tb_key_value`;  
  47. create table `tb_key_value`(  
  48.     uid bigint(22) not null,  
  49.     k varchar(100),  
  50.     v varchar(100)  
  51. );  

我们插入了10条数据到tb_user中,如果tb_user中未定义的字段如address,则放置在tb_user_param中,该表是个key_value的结构,由uid+code定位。

后面我们要做的是,将根据tb_user中的uid找到tb_user_param中相关记录后,将key_value信息转移到tb_key_value表中,为达到演示效果,我们使用嵌套游标操作数据,代码如下:

Sql代码  收藏代码
  1. drop procedure if exists `sp_nested_cursor`;  
  2. create procedure `sp_nested_cursor`()  
  3. begin  
  4.     declare v_uid bigint(22);  
  5.     declare v_code varchar(100);  
  6.     declare v_value varchar(100);  
  7.     declare _done TINYINT(1) default 0;  
  8.     declare cur_user cursor for select id from `tb_user`;  
  9.     declare continue handler for not found set _done = 1;  
  10.       
  11.     open cur_user;  
  12.     loop_xxx:loop  
  13.         fetch cur_user into v_uid;  
  14.         if _done=1 then  
  15.             leave loop_xxx;  
  16.         end if;  
  17.         begin  
  18.             declare _inner tinyint(1) default 0;  
  19.             declare cur_param cursor for select code, value   
  20.                                          from `tb_user_param`   
  21.                                          where user_id=v_uid;  
  22.             declare continue handler for not found set _inner = 1;   
  23.             open cur_param;  
  24.             loop_yyy:loop  
  25.                 fetch cur_param into v_code, v_value;  
  26.                 if _inner=1 then  
  27.                     leave loop_yyy;  
  28.                 end if;  
  29.                 insert into tb_key_value values (v_uid, v_code, v_value);  
  30.             end loop;  
  31.             commit;  
  32.         end;  
  33.     end loop;  
  34. end;  
  35. call `sp_nested_cursor`();  
 如果想跟踪上面程序的执行过程,可以借助MySQL Debugger工具调试学习。

转载地址: http://shansun123.iteye.com/blog/1026084 

0 0
原创粉丝点击