我必须从Old_Table中将数据插入New_Table中 旧表中有大约10k的记录 我的Old_Table数据如下所示
ID Structure
001 APC , AMC , Self Service, Change of Billing Address, EPC,
002 APC , Self Service, EPC, OBA,
003 EPC,
004 AMC
005 Self Service
New_Table应该如下所示
ID Structure
001 APC
001 AMC
001 Self Service
001 Change of Billing Address
001 EPC
001 (empty/space)
002 APC
002 Self Service
002 EPC
002 OBA
002 (empty/space)
003 EPC
003 (empty/space)
004 AMC
005 Self Service
解决问题和迁移数据的最简单方法是什么 感谢
答案 0 :(得分:1)
尝试使用以下sql来拆分值。表ALL_TABLES仅用作驱动程序。您可以使用每个表,其中包含逗号之间具有单个值的行数。
insert into a values (1, 'A,B,C')
/
insert into a values (2, 'E,F')
/
insert into a values (3, 'ASD,BSF,BERT,BROT')
/
select * from a
/
select id, s, v, b, substr(s, v+1, b-v-1)
from (
select id, s, instr(s, ',', 1, n) v, instr(s, ',', 1, n+1) b
from (
select a.id id, ','||a.s||',' s, c.n
from a a, (select rownum n from all_tables) c
))
where v > 0 and b > 0
/
这是一个链接到sql小提琴来玩http://www.sqlfiddle.com/#!4/f6ab9/2/4
答案 1 :(得分:0)
我找到了问题的答案。以下查询对我有用。
insert into New_Table (ID, Structure)
(select ID, trim(Structure)
from (
WITH TT AS
(SELECT ID, Structure FROM Old_Table)
SELECT ID, substr(str,
instr(str, ',', 1, LEVEL) + 1,
instr(str, ',', 1, LEVEL + 1) -
instr(str, ',', 1, LEVEL) - 1) Structure
FROM (SELECT ID, rownum AS r,
','|| Structure||',' AS STR
FROM TT )
CONNECT BY PRIOR r = r
AND instr(str, ',', 1, LEVEL + 1) > 0
AND PRIOR dbms_random.STRING('p', 10) IS NOT NULL)
where trim(Structure) is not null);