连接来自同一列 Oracle 11G 的大量文本数据
Concatenating huge amount of text data from the same column Oracle 11G
我使用的是企业版 11.2.0.4.0。
我想将每个学生的评论从 table 连接成一个巨大的评论,然后将其输出(假脱机)到一个文件中。
我的 table 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
我该怎么做?请帮忙。
既然你有 11.2,你应该可以使用 listagg。
SELECT LISTAGG(comments, ',') WITHIN GROUP (ORDER BY sid) AS BIGCOMMENT
FROM teacher_comments
GROUP BY sid;
我不记得 listagg returns 是否是 clob,但如果是,我会在使用上面的 SQL 之前听从 Jon Heller 的建议。不过,您肯定想停止使用 wm_concat。
ORA-01489: result of string concatenation is too long
LISTAGG returns VARCHAR2 (or RAW)
因此限制为 4000 字节。 SQL*Plus
默认情况下有 linesize 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>
我使用的是企业版 11.2.0.4.0。 我想将每个学生的评论从 table 连接成一个巨大的评论,然后将其输出(假脱机)到一个文件中。
我的 table 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
我该怎么做?请帮忙。
既然你有 11.2,你应该可以使用 listagg。
SELECT LISTAGG(comments, ',') WITHIN GROUP (ORDER BY sid) AS BIGCOMMENT
FROM teacher_comments
GROUP BY sid;
我不记得 listagg returns 是否是 clob,但如果是,我会在使用上面的 SQL 之前听从 Jon Heller 的建议。不过,您肯定想停止使用 wm_concat。
ORA-01489: result of string concatenation is too long
LISTAGG returns VARCHAR2 (or RAW)
因此限制为 4000 字节。 SQL*Plus
默认情况下有 linesize 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>