直接上代码:
1 CREATE DEFINER=`root`@`localhost` PROCEDURE `sos`( ) 2 BEGIN 3 -- 创建一个临时表 4 DROP TABLE IF EXISTS fids_temp; 5 CREATE temporary TABLE IF NOT EXISTS fids_temp( 6 fid VARCHAR (64), 7 deviceIds VARCHAR(64) 8 ) ; 9 10 BEGIN 11 -- 声明游标 12 declare a VARCHAR(64); 13 declare b VARCHAR(64); 14 15 -- 声明在最后一行标记 16 DECLARE flag int DEFAULT 0; 17 18 -- 声明一个多个结果集 19 DECLARE fids_deviceId CURSOR FOR SELECT fid,device_id FROM device_fids_copy WHERE `status`= 'ENABLE' AND device_id like 'A12601A%' LIMIT 0,50 ; 20 21 -- 设置结束标识 22 DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET flag = 1; 23 24 -- 打开游标 25 open fids_deviceId; 26 27 -- 读取游标第一行 28 FETCH fids_deviceId into a,b; 29 30 WHILE flag<> 1 DO 31 -- 插入临时表 32 INSERT into fids_temp (fid,deviceIds) VALUES (a,b); 33 -- 读取下一行 34 FETCH fids_deviceId into a,b; 35 36 END WHILE; 37 38 -- 有开就有关 39 close fids_deviceId; 40 41 SELECT * FROM fids_temp; 42 43 -- SELECT COUNT(*) FROM fids_temp; 44 45 end; 46 47 48 END
结果:(部分)