2012-01-13 61 views
1

如何将一个表中多行的数据合并到新表中的单个列中?如何在新表中将多行数据合并到单列中?

create table new_paragraphs 
(
    id NUMBER 
    paragraph CLOB 
); 

create table old_paragraphs 
(
    id 
    paragraph CLOB 
); 

merge into new_paragraphs a 
using (select * from old_paragraphs) b 
on (id = id) 
when matched then 
update set a.paragraph = a.paragraph || b.paragraph; 
-- Results in error: unable to get a stable set of rows in the source tables 

上面抛出异常。

+0

其实,这导致'ORA-00918:列含糊defined'。 – 2012-01-13 21:27:26

回答

0

如果id均至少* old_paragraphs *(或者,如果它是每个ID唯一在发现了一个主键,它的工作* new_paragraph *)

除此之外,要在on (id = id)使用别名,以便它读取on (a.id = b.id)

merge into new_paragraphs a 
using (select * from old_paragraphs) b 
on (a.id = b.id) 
when matched then 
update set a.paragraph = a.paragraph || b.paragraph; 
0

你为什么在这里做MERGE?为什么不是一个简单的UPDATE(假设ID是两个表的主键)

UPDATE new_paragraphs a 
    SET paragraph = (select a.paragraph || b.paragraph 
         from old_paragraphs b 
        where a.id = b.id) 
WHERE EXISTS (SELECT 1 
       FROM old_paragraphs b 
       WHERE a.id = b.id) 
+0

old_paragraphs中的每个id有超过1行。当我运行你的查询时,我得到异常ORA-01427:单行子查询返回多个行 – firebird 2012-01-13 19:29:32

+0

@firebird - 确定。所以'ID'不是'OLD_PARAGRAPHS'的主键?您是否关注附加旧段落数据的顺序? – 2012-01-13 19:32:18

相关问题