我正在使用Enterprise Edition版本11.2.0.4.0。 我想将表中的每个学生的注释连接成一个巨大的注释,然后将其输出(假脱机)到文件中。
我的表TEACHER_COMMENTS有列:
SID INT
COMMENTS VARCHAR2(4000)
数据如下所示:
SID COMMENTS
1 It has truly been a pleasure getting to know your child this quarter. our child has made great progress across the curriculum since the beginning of the school year.
1 Your child has done a very nice job this quarter, taking pride in her work and completing assignments with quality in mind.. Your child has made very good academic and/or social progress this quarter.
我想将同一个学生的评论合并为一个巨大的评论。
这是我的代码:
SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON
set underline off
set pages 0
set lines 1500
set feedback off
set autop off
set term off
set ver off
set timing off
set time off
spool E:\Test\Comments.csv
SELECT wm_concat(COMMENTS) FROM TEACHER_COMMENTS t GROUP BY SID;
spool off;
exit;
这只是输出一行评论,而不是整个巨大的评论。
我也尝试过使用LIST_AGG
SELECT LISTAGG(COMMENT, ' ') WITHIN GROUP (ORDER BY COMMENT) AS All_Comments
FROM TEACHER_COMMENTS t
GROUP BY SID
这会引发错误:
ORA-01489: result of string concatenation is too long
我该怎么做?请帮忙。
答案 0 :(得分:1)
ORA-01489:字符串连接的结果太长
LISTAGG 返回VARCHAR2 (or RAW)
,因此限制为4000字节。默认情况下,SQL*Plus
的行大小为80。
一种可能的解决方案是使用XMLAGG
并将LONG
设置为较高的值。让我们看看它是如何运作的 -
SQL> SET LONG 2000000000
SQL> WITH DATA AS(
2 SELECT 1 SID, 'It has truly been A pleasure getting TO know your CHILD
3 this quarter. our CHILD has made great progress across THE curriculum
4 since THE beginning OF THE school YEAR.' comments FROM dual UNION ALL
5 SELECT 1, 'Your child has done a very nice job this quarter, taking pride
6 in her work and completing assignments with quality in mind.. Your child
7 has made very good academic and/or social progress this quarter.' FROM dual
8 )
9 SELECT rtrim(xmlagg(XMLELEMENT(e,comments,',').EXTRACT('//text()')
10 ORDER BY sid).GetClobVal(),',') as long_comments
11 FROM DATA
12 /
LONG_COMMENTS
--------------------------------------------------------------------------------
It has truly been A pleasure getting TO know your CHILD
this quarter. our CHILD has made great progress across THE curriculum
since THE beginning OF THE school YEAR.,Your child has done a very nice job this
quarter, taking pride
in her work and completing assignments with quality in mind.. Your child
has made very good academic and/or social progress this quarter.
SQL>
答案 1 :(得分:-2)
由于你有11.2,你应该可以使用listagg。
SELECT LISTAGG(comments, ',') WITHIN GROUP (ORDER BY sid) AS BIGCOMMENT
FROM teacher_comments
GROUP BY sid;
我不记得listagg是否返回clob,但如果确实如此,我会在使用上面的SQL之前遵循Jon Heller的建议。你肯定想停止使用wm_concat。