作者m4141922 (Joker.T)
看板Database
标题[SQL ] Mysql cursor存取问题
时间Wed Feb 8 12:57:17 2017
资料库名称:MySQL
资料库版本:5.7.15
内容/问题描述:
我需要把某个表格的内容拆开记录到新的表格、大致上如下
栏A 栏B
----------------
25 123A;275B;345A;
27 467B;123A;
----------------
栏A 栏B
----------------
25 123A
25 275B
25 345A
27 ......
----------------
但是目前用CURSOR操作时会发生存取值只存了栏B的问题...(栏A的值是NULL)
请帮我检查哪里出了问题><""
declare done INT DEFAULT FALSE;
declare B varchar(30);
declare A varchar(2);
declare length int;
declare i int;
declare cur cursor for select A,B from table_1;
declare continue handler for not found set done=true;
open cur;
read_loop: LOOP
FETCH cur INTO A,B;
IF done THEN
LEAVE read_loop;
END IF;
set length=length(B);
set i=1;
while_label :while (i<=length/5) do
insert into table_2 (A,B)
values (A,substring(B,(i-1)*5+1,4);
set i=i+1;
end while;
END LOOP;
CLOSE cur;
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.27.47.196
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1486529840.A.88B.html