0

当我使用 wm_concat 将几个元组连接成一个元组时。

例如

ID 项目
1 '苹果'
2 '桃子'
2 '香蕉'

 declare 
  v_name_l: varchar(100);
  v_name_b: varchar(100);
begin
     select wm_concat(Items) into v_name_l from Item group by ID having count(*)=1;
     select wm_concat(Items) into v_name_b from Item group by ID having count(*)=2;
     execute immediate ('Insert into  apr values('||v_name_l||','||v_name_b||')');
end;

另一个表 apr 结构是
Name1 Name2

name1 和 nam2 是 varchar(100);

有一个错误引发

*execute immediate execute immediate ('Insert into  apr values('||v_name_l||','||v_name_b||')')"

   ORA-00913: too many values

我认为 wm_concat 将多行连接到一个字符串中;
我该如何解决?

4

1 回答 1

0

I think nothing wrong with wm_concat queries. You just need to escape quotes. Because you are referencing your query as string in this case lets say:

v_name_l is 'apple, peach, banana' and v_name_b is 'x, y, z'

So when you define as current query:

Insert into  apr values('||v_name_l||','||v_name_b||')

Oracle translates to:

Insert into apr values(apple, peach, banana, x, y, z)

And database recognize every comma separated value as a column but the table has just two column instead of 6. If you escape quotes:

Insert into  apr values('''||v_name_l||''','''||v_name_b||''');

then oracle will read like:

Insert into apr values('apple, peach, banana', 'x, y, z');

I hope that was the reason:)

于 2012-11-27T21:40:29.483 回答