MYSQL游标嵌套循环示例
来源:互联网 发布:衬衫品牌 知乎 编辑:程序博客网 时间:2024/04/30 02:24
本文将举例演示游标的嵌套用法,首先建造表结构,如下:
- drop table if exists `tb_user`;
- create table tb_user (
- id bigint(22) not null auto_increment primary key,
- name varchar(20) not NULL,
- age tinyint(3),
- gmt_create date,
- gmt_modified date
- );
- drop table if exists `tb_user_param`;
- create table tb_user_param (
- id bigint(22) not null auto_increment primary key,
- user_id bigint(22) not null,
- code varchar(100) not null,
- value varchar(1000),
- gmt_create DATE,
- gmt_modified date
- );
- drop procedure if exists `sp_init_data`;
- create procedure `sp_init_data`()
- begin
- declare v_i bigint(22) default 0;
- declare v_uid bigint(22);
- declare v_nick varchar(20) default 'lanbo_';
- declare v_code_1 varchar(20) default 'address';
- declare v_code_2 varchar(20) default 'phone';
- declare v_code_3 varchar(20) default 'wangwang_id';
- declare v_value_1 varchar(20) default 'HZ.XiHu';
- declare v_value_2 varchar(20) default '1875757198';
- declare v_value_3 varchar(20) default 'shansun_';
- while v_i < 10 do
- set v_i = v_i + 1;
- insert into tb_user values (null, concat(v_nick, v_i), 23, now(), now());
- select LAST_INSERT_ID() into v_uid;
- insert into tb_user_param values(null, v_uid, v_code_1, v_value_1, now(), now());
- insert into tb_user_param values(null, v_uid, v_code_2, concat(v_value_2, v_i), now(), now());
- insert into tb_user_param values(null, v_uid, v_code_3, concat(v_value_3, v_i), now(), now());
- end while;
- commit;
- end;
- call sp_init_data();
- drop table if exists `tb_key_value`;
- create table `tb_key_value`(
- uid bigint(22) not null,
- k varchar(100),
- v varchar(100)
- );
我们插入了10条数据到tb_user中,如果tb_user中未定义的字段如address,则放置在tb_user_param中,该表是个key_value的结构,由uid+code定位。
后面我们要做的是,将根据tb_user中的uid找到tb_user_param中相关记录后,将key_value信息转移到tb_key_value表中,为达到演示效果,我们使用嵌套游标操作数据,代码如下:
- drop procedure if exists `sp_nested_cursor`;
- create procedure `sp_nested_cursor`()
- begin
- declare v_uid bigint(22);
- declare v_code varchar(100);
- declare v_value varchar(100);
- declare _done TINYINT(1) default 0;
- declare cur_user cursor for select id from `tb_user`;
- declare continue handler for not found set _done = 1;
- open cur_user;
- loop_xxx:loop
- fetch cur_user into v_uid;
- if _done=1 then
- leave loop_xxx;
- end if;
- begin
- declare _inner tinyint(1) default 0;
- declare cur_param cursor for select code, value
- from `tb_user_param`
- where user_id=v_uid;
- declare continue handler for not found set _inner = 1;
- open cur_param;
- loop_yyy:loop
- fetch cur_param into v_code, v_value;
- if _inner=1 then
- leave loop_yyy;
- end if;
- insert into tb_key_value values (v_uid, v_code, v_value);
- end loop;
- commit;
- end;
- end loop;
- end;
- call `sp_nested_cursor`();
转载地址: http://shansun123.iteye.com/blog/1026084
0 0
- MYSQL游标嵌套循环示例
- mysql游标嵌套循环
- mysql游标嵌套循环
- Mysql存储过程循环内嵌套使用游标示例
- MySQL游标循环示例
- Mysql----游标的嵌套循环
- MYSQL游标嵌套循环 动态游标
- mysql存储过程示例(使用嵌套游标)
- mysql存储过程游标嵌套示例
- MYSQL 循环嵌套(自定义函数,游标)
- MySQL游标嵌套循环存储过程
- MySQL游标嵌套循环存储过程
- sql循环嵌套、游标使用、循环结果集示例
- SQL SERVER中SQL中游标嵌套循环的示例
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- mysql存储过程 例子 游标cursor 循环嵌套 事物
- MYSQL存储过程,while循环和游标的嵌套使用
- bzoj2245(分段加边典型费用流)
- 销售订单的行项目里条件的增强
- emqttd--------mqttd-benchmark-------Erlang安装
- 使用HMTL5 API监控前端性能
- IP、TCP和DNS与HTTP的密切关系
- MYSQL游标嵌套循环示例
- SPI框架
- Windows - Review application whether 32-bit or 64bit
- TextView局部文字样式美化(SpannableStringBuilder)
- XML-Schema学习笔记
- iOS提交版本时苹果官方审核拒绝原因详解
- Scala中常见的容器 List
- java虚拟机性能监控与故障处理工具
- 常用的数据统计Sql 总结