[英]How to migrate data from the old table to the new table in oracle by removing the comma from the column values
我必須將數據從Old_Table插入New_Table
舊表中大約有1萬條記錄
我的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
解決問題和遷移數據的最簡單方法是什么。
謝謝
嘗試使用以下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上播放
我找到了問題的答案。 下面的查詢確實為我工作。
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.