我有一张这样的表:
columnId1 => "word1,word2,word3"
columnId3 => "word4,word5,word6"
我想创建一个这样的表:
columnId1 word1
columnId1 word2
columnId1 word3
columnId2 word4
columnId2 word5
columnId2 word6
我怎样才能在PLSQL中做到这一点?
答案 0 :(得分:2)
create table testtable(col1 varchar2(50) , col2 varchar2(50));
insert into testtable (col1, col2)
with commadata as(
select 'word1,word2,word3' columnid1, 'word4,word5,word6' columnid2
from dual
)
select regexp_substr( columnid1
, '[^,]+'
,1
,level) as parsed_value
,
regexp_substr( columnid2
, '[^,]+'
,1
,level) as parsed_value
from commadata
connect by level <= REGEXP_COUNT( columnid1 , '[,]' )+1
;
select * from testtable;
COL1 COL2
-------------------------------------------------- --------------------------------------------------
word1 word4
word2 word5
word3 word6
这假设您使用10g并且可以访问正则表达式。这应该足以让你继续创建你的语句,注意它是脆弱的,如果columnId1和columnId2有不同的逗号等等,它可能会中断。
答案 1 :(得分:1)
很久以前我为这种需要写了一个自定义函数。我正在研究一个通常希望在数据库中存储逗号分隔值的应用程序。我必须创建一个函数来选择它,就像它是一个表和一个反向函数,以便应用程序很高兴。我似乎无法找到我最终提出的功能,但是我把它从汤姆的回复写到了我在这里问的一个问题:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2189860818012#28667254837962