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

Oracle 如何将多行数据合并到新表中的单个列中?,oracle,oracle10g,Oracle,Oracle10g,如何将一个表中多行的数据合并到新表中的一列 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 upda

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

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

上面抛出了一个异常。

为什么要在这里进行
合并?为什么不进行一次简单的
更新
(假设
ID
是两个表的主键)


如果
id
是至少*旧段落*中的主键(或者如果它对于*新段落*中找到的每个id都是唯一的),那么它就可以工作了

除此之外,您希望在(id=id)
上的
中使用别名,以便它在(a.id=b.id)
上读取


在旧的_段落中,每个id有超过1行。当我运行您的查询时,我得到异常ORA-01427:单行子查询返回多个row@firebird-好的。所以
ID
不是
旧段落的主键
?您是否关心旧段落中的数据的附加顺序?实际上,它会导致
ORA-00918:列定义不明确
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)
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;