我得到了一些帮助,并被引导到这个页面和这个解释,其中应该包含一种聚合事物的有效方法。
它建议使用 deCOLLECT
函数和其他一些自定义的东西。我正在尝试与它相处,但错误消息(加上我的新手)并不是最有帮助的。
功能:
CREATE OR REPLACE TYPE t_varchar2_tab AS TABLE OF VARCHAR2(4000);
CREATE OR REPLACE FUNCTION tab_to_string (
p_varchar2_tab IN t_varchar2_tab,
p_delimiter IN VARCHAR2 DEFAULT ',')
RETURN VARCHAR2 IS
l_string VARCHAR2(32767);
BEGIN
FOR i IN p_varchar2_tab.FIRST .. p_varchar2_tab.LAST LOOP
IF i != p_varchar2_tab.FIRST THEN
l_string := l_string || p_delimiter;
END IF;
l_string := l_string || p_varchar2_tab(i);
END LOOP;
RETURN l_string;
END tab_to_string;
我的测试:
with my_table as
(
select 'user1' as usrid, 'ab' as prodcode from dual union
select 'user1' as usrid, 'bb' as prodcode from dual union
select 'user1' as usrid, 'a' as prodcode from dual union
select 'user2' as usrid, 'db' as prodcode from dual union
select 'user2' as usrid, 'b' as prodcode from dual union
select 'user2' as usrid, 'bfdd' as prodcode from dual
)
select
usrid,
tab_to_string(CAST(COLLECT(prodcode) AS t_varchar2_tab)) AS codes
from
my_table
group by
usrid
会给我一个ORA-06553: PLS-306: wrong number or types of arguments in call to 'TAB_TO_STRING'
这几乎是我一开始提到的来源的复制和过去,并且该功能对我来说很有意义..我错过了什么?
谢谢!
[编辑] Codo 认为问题之一是 Oracle 将 char 理解'a'
为char而不是varchar。这就把问题带到了真正的问题上。我更新了它,所以它是专注的。