你必须避免wm_concat
函数,因为它没有文档记录并且在 Oracle 8i 时代被发现作为解决方法。
自从 Tom Kyte 发现的具有自定义聚合函数的旧方法时代以来here http://asktom.oracle.com/pls/apex/f?p=100:11:0%3a%3a%3a%3aP11_QUESTION_ID:15637744429336有一些新的解决方法,如下面的示例所示。
全部转载于这个 SQL 小提琴 http://www.sqlfiddle.com/#!4/5eef8/2.
解决方法 1 - LISTAGG 函数,适用于 11g:
select listagg(object_id,',') within group (order by rownum) id_string
from cr_object_group_entries_vw
解决方法 2 - SYS_CONNECT_BY_PATH,自 10g 起有效:
select id_string from (
select rn, substr(sys_connect_by_path(object_id, ','),2) id_string
from (select object_id, rownum rn from cr_object_group_entries_vw)
start with rn = 1
connect by prior rn + 1 = rn
order by rn desc
)
where rownum = 1
解决方法 3 - XMLAGG,自 10g 起有效:
select replace(
replace(
replace(
xmlagg(xmlelement("x",object_id)).getStringVal(),
'</x><x>',
','
),
'<x>',
''
),
'</x>',
''
) id_string
from cr_object_group_entries_vw
附:我不知道具体在哪些Oracle版本中sys_connect_by_path
and xmlagg
被引入,但两者都在 10.2.0.4.0 上运行良好