游标的使用

游标:

  类似于数组的下标。

declare cur_test CURSOR for select id ,username from erp_manages; 定义游标;

declare continue handler FOR SQLSTATE '02000' set done=1;   当数据扫描到低端标志结束;

DROP PROCEDURE if exists test_cursor;  Mysql存储过程无法更改内容,只能删除重建。

 

 1 delimiter |
 2 
 3 create procedure test_cursor(in param int(10),out result varchar(90))
 4 
 5 begin
 6         declare cid int(20);
 7         declare cusername varchar(20);
 8         declare done int;
 9         declare cur_test CURSOR for select id ,username from erp_manages;
10         declare continue handler FOR SQLSTATE '02000' set done=1;
11         if param then
12                 select concat_ws(',',id,username)into result from erp_manages where id=param;
13         else
14                 open cur_test;
15                 repeat
16                         fetch cur_test into cid,cusername;
17                         select concat_ws(',',result,cid,cusername)into result;
18                 until done end repeat;
19                 close cur_test;
20         end if;
21 end; |

 


 

posted @ 2014-07-08 17:38  UCanBeFree  阅读(262)  评论(0)    收藏  举报