- 浏览: 1061420 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (399)
- C++ (39)
- Java (74)
- Java界面开发学习笔记 (4)
- Java用户的c++之旅 (0)
- 自言自语 (12)
- DSP (1)
- MCU (0)
- CG (0)
- Jabber (0)
- Gloox (0)
- Linux (11)
- Windows (19)
- Networks (4)
- Jobs (0)
- PHP (1)
- JSP (2)
- 生活 (35)
- C (2)
- Qt4 (2)
- C# (50)
- WPF (5)
- ASP (2)
- FLEX (47)
- SQL (20)
- JavaScript (12)
- SharePoint (6)
- GWT (1)
- Dojo (9)
- HTML (11)
- Others (7)
- 如何安装配置系列 (7)
- UML (2)
- Android (3)
- alibaba (1)
最新评论
-
zxjlwt:
学习了http://surenpi.com
Firefox插件开发: Hello World! -
ylldzz:
楼主知道MVEL怎么调试么
MVEL简介及快速使用 -
blueman2012:
您好,可否提供源码下载,我把您的代码贴过来后,好多报错的,谢谢 ...
Log4J日志解析 -
svygh123:
你的游标都没有关闭呢!
MYSQL游标嵌套循环示例 -
dizh:
写的很好啊
MVEL简介及快速使用
本文将举例演示游标的嵌套用法,首先建造表结构,如下:
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`();如果想跟踪上面程序的执行过程,可以借助MySQL Debugger工具调试学习。
发表评论
-
[转]利用Oracle的row_number() over函数消除重复的记录
2011-05-08 23:24 19671.select d.id,d.outer_code ... -
MySQL存储过程☞错误处理相关知识总结
2011-03-29 08:51 1132MySQL错误码:http://dev.mysql.com/d ... -
SQL Manager 2010 for Mysql Free Edition
2011-03-26 20:43 2803最近用了Windows Server 20 ... -
Oracle 和 mysql 的一些简单命令对比参照
2011-01-22 00:43 1046Oracle mysql 对比版 ... -
MySQL计算两个日期相差的天数
2011-01-22 00:40 2446select to_days('2011-05-11') - ... -
PL/SQL块中如何调用DDL语句(sql)
2011-01-11 11:13 1864在PL/SQL块中不能直接调用DDL语句,可以利用下面的 ... -
ORA-01810: format code appears twice
2010-11-16 14:49 1374Oralce中的to_date()函数用于将字符串转换为 ... -
These query results are not updateable.Include the ROWID...
2010-10-19 14:36 8242http://www.zlsoft.com/tec ... -
MSSQL 2005删除所有表的语句
2010-05-05 13:51 1157godeclare @tbname varchar(250)d ... -
”SQL BPA command line has encountered a problem and needs to close“的解决方法
2010-04-28 11:17 1610There should exist on the s ... -
安装SQL Server 2005时,出现“Edition Change Check”警告的解决方法
2010-04-28 11:15 1745安装VS2008之后,又安装SQL Server 2005时, ... -
SQL SERVER 2005 中sa用户登录和新建用户
2010-04-13 12:25 3080'用户 'sa' 登录失败。该用户与可信 SQL Server ... -
SQL Server 2005 Remote Connection is not enabled 解决方法
2010-04-13 10:35 1288go to sql server configuration ... -
MySQL汉字乱码问题的几种解决方法
2009-11-14 17:04 16771.在安装Mysql时其实可能选择使用GBK来处理汉字,由于以 ... -
Access denied for user 'root'@'localhost' (using password: YES)
2009-07-24 13:00 8614Access denied for user 'root'@' ... -
SQL语句摘录--FOR MSSQL
2009-07-21 12:56 1135下列语句部分是Mssql语句,不可以在access中使用。 ... -
MySQL时间列自动插入当前时间
2009-07-20 22:28 1983注意:用current_timestamp,不过这个默认值只用 ... -
MySQL,SQL Server,Oracle中连接字符串的操作
2009-06-08 21:21 1599MySQL: select concat(ename, ' i ... -
MySQL怎么查询排序后结果集的前[N, M]条记录
2009-06-03 13:23 9758今天群里有人提了这么一个问题,原话是: “表T(a,b,c,d ...
相关推荐
Mysql游标(循环操作)
mysql游标嵌套[文].pdf
本节主要介绍了Mysql存储过程循环内如何嵌套使用游标,详细实现如下,需要的朋友不要错过
一个存储过程的示例,简单展示了如下方面: 1,异常处理,事务回滚。 2,错误消息返回。 3,游标嵌套。 4,对于嵌套游标STATUS的互相影响问题的解决办法《注意001和002的注释位置,fetch的位置》。 对于初学者,...
SQL对游标嵌套的应用 学校宿舍管理系统(实际上是个子系统): 包括以下几个表: 楼栋表;楼层表;房间表;学员住宿表 脚本如下: --楼栋表
mysql游标使用的整个过程为: 1.创建游标 代码如下:DECLARE calc_bonus CURSOR FOR SELECT id, salary, commission FROM employees; 2.打开游标 代码如下:OPEN calc_bonus; 3.使用游标 代码如下:FETCH calc_bonus ...
NULL 博文链接:https://modiliany.iteye.com/blog/786230
NULL 博文链接:https://yuhuiblog695685688425687986842568269.iteye.com/blog/2424995
带游标的mysql存储过程例子。
ORACLE 游标使用示例,给大家可以共享一下。
Mysql从5.0开始支持存储过程和trigger,游标在递归树结构中非常好用
完整的SQL SERVER双层游标嵌套示例
NULL 博文链接:https://fangyong2006.iteye.com/blog/768291
[mysql游标的用法及作用] 例子: 当前有三张表A、B、C其中A和B是一对多关系,B和C是一对多关系,现在需要将B中A表的主键存到C中; 常规思路就是将B中查询出来然后通过一个update语句来更新C表就可以了,但是B表中有...
oracle 游标FOR循环
众所周知,MySQL中的游标循环操作常用的有三种,LOOP,REPEAT,WHILE.三种循环,方式大同小异.以前从没用过,所以记下来,方便以后查阅. 1.REPEAT 代码如下: REPEAT Statements; UNTIL expression END REPEAT demo ...
FETCH NEXT FROM stu_corsor --下一条记录 FETCH PRIOR FROM stu_corsor --上一条记录
Mysql存储过程游标触发器