MYSQL遊標嵌套循環示例

本文將舉例演示遊標的嵌套用法,首先建造表結構,如下:

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 

發佈了40 篇原創文章 · 獲贊 20 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章