如何使用MYSQL游标
//建立表结构
create table person(name varchar(10));
insert into person values(¹a¹);
insert into person values(¹b¹);
insert into person values(¹c¹);
insert into person values(¹d¹);
insert into person values(¹e¹);
//初始化
drop procedure if exists cursorTest
//建立
CREATE PROCEDURE cursorTest()
BEGIN
//定义变量
declare name1 varchar(10) default ¹¹ ;
declare name2 varchar(100) default ¹¹ ;
declare mycursor CURSOR FOR select name from person;
declare CONTINUE HANDLER FOR SQLSTATE ¹02000¹ SET tmpname = null;
//打开游标
OPEN mycursor;
//遍历游标
FETCH mycursor INTO name1;
//把游标查询出的 name 都加起并用 ; 号隔开
WHILE( name1 is not null) DO
set name1 = CONCAT(name1, ";") ;
set name2 = CONCAT(name2, name1) ;
FETCH mycursor INTO name1;
END WHILE;
CLOSE mycursor;
select mycursor;
END;
//调用游标
call mycursor()
运行结果:
mysql> call mycursor()
+--------------------------------------+
| name2 |
+--------------------------------------+
| a;b;c;d;e; |
+--------------------------------------+
1 row in set (0.01 sec)
- 默认分类(20)
- J2EE(25)
- Java(56)
- PHP(55)
- SEO(10)
- 网页设计(20)
- 网站建设(37)
- 数据库(7)
- JavaScript(17)
- JQuery(6)
- MySQL(20)
- SQL Server(6)
- Access(1)
- Oracle(6)
- office(6)
- Dreamweaver(4)
- Photoshop(12)
- Flash(9)
- Fireworks(13)
- CSS(14)
- HTML(4)
- .NET(7)
- ASP(2)
- DB2(1)
- Ajax(2)
- Linux(12)
- Struts(7)
- Hibernate(8)
- Spring(2)
- Jsp(22)
- Asp(8)
- C#(3)
- C++(1)
- 网络安全(5)
- 软件工程(7)
- XML(1)
- English(2)
- 计算机等级考试(2)
- 计算机病毒(4)
- 个人日志(76)
- 互联网(15)
- ActionScript(10)
- Android(3)
- 数据结构与算法(1)
- 游戏策略(3)
- 美文翻译(2)
- 编程开发(19)
- 计算机应用(4)
- 计算机(10)
- Unity3d(6)
- 其他(1)
- egret(1)