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

时间:2012-01-13 16:59:17

标签: 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
update set a.paragraph = a.paragraph || b.paragraph;
-- Results in error: unable to get a stable set of rows in the source tables

以上引发了异常。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

如果id至少是* old_paragraphs *中的主键(或者对于* new_paragraph *中找到的每个id都是唯一的),它会起作用

除此之外,你想在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;