如果结果大于 4000 字节,则使用 CLOB。
SQL> create table web_views_tab(SESSION_ID number, SESSION_DT date, C_IP varchar2(20), CS_USER_AGENT varchar2(10), web_link varchar2(100));
Table created.
SQL> insert into web_views_tab
2 select rownum, trunc(sysdate), '127.0.0.1', 'Mozilla', 'http://foo.bar.com/asdakjdlkajdlkajsd/asdjaldjklja'
3 from dual
4 connect by level <= 3000;
3000 rows created.
SQL> CREATE OR REPLACE TYPE t_varchar2_tab AS TABLE OF VARCHAR2(4000);
2 /
Type created.
SQL> CREATE OR REPLACE FUNCTION tab_to_string (p_varchar2_tab IN t_varchar2_tab,
2 p_delimiter IN VARCHAR2 DEFAULT ',') RETURN VARCHAR2 IS
3 l_string VARCHAR2(32767);
4 BEGIN
5 FOR i IN p_varchar2_tab.FIRST .. p_varchar2_tab.LAST LOOP
6 IF i != p_varchar2_tab.FIRST THEN
7 l_string := l_string || p_delimiter;
8 END IF;
9 l_string := l_string || p_varchar2_tab(i);
10 END LOOP;
11 RETURN l_string;
12 END tab_to_string;
13 /
Function created.
SQL> create or replace view FINAL_WEB_LOG
2 as
3 select SESSION_DT,
4 C_IP,
5 CS_USER_AGENT,
6 tab_to_string(CAST(COLLECT(web_link) AS t_varchar2_tab)) WEBLINKS
7 from web_views_tab
8 group by C_IP, CS_USER_AGENT, SESSION_DT;
View created.
SQL> select * from FINAL_WEB_LOG;
select * from FINAL_WEB_LOG
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "TEST.TAB_TO_STRING", line 9
ORA-06512: at line 1
所以我们将函数输出重新定义为 clob 并稍微改变函数:
SQL> CREATE OR REPLACE FUNCTION tab_to_string (p_varchar2_tab IN t_varchar2_tab,
2 p_delimiter IN VARCHAR2 DEFAULT ',') RETURN clob IS
3 l_string clob;
4 BEGIN
5 dbms_lob.createtemporary(l_string, true, dbms_lob.call);
6 dbms_lob.open(l_string, dbms_lob.lob_readwrite);
7 FOR i IN p_varchar2_tab.FIRST .. p_varchar2_tab.LAST LOOP
8 IF i != p_varchar2_tab.FIRST THEN
9 dbms_lob.writeappend(l_string, length(p_delimiter), p_delimiter);
10 END IF;
11 dbms_lob.writeappend(l_string, length(p_varchar2_tab(i)), p_varchar2_tab(i));
12 END LOOP;
13 dbms_lob.close(l_string);
14 RETURN l_string;
15 END tab_to_string;
16 /
Function created.
SQL>
SQL> select * from FINAL_WEB_LOG;
SESSION_D C_IP CS_USER_AG
--------- -------------------- ----------
WEBLINKS
--------------------------------------------------------------------------------
14-DEC-12 127.0.0.1 Mozilla
http://foo.bar.com/asdakjdlkajdlkajsd/asdjaldjklja,http://foo.bar.com/asdakjdlka