ORACLE ORA-01489: the reason and solution of the result of string concatenation is too long

The following SQL, when using listagg for grouping and splicing, it often reports an ora-01489 error. The main reason for this error is: Oracle's limitation on the length of character variables. Under normal circumstances, the length of varchar2 type variables defined by Oracle should not exceed 4000. Bytes, if necessary, can be converted to long or clob type. 

solution:

  Method 1: Customize the connection function (transfer by function)

 

-- 自定义一个 tab_varchar2数据类型

  • CREATE TYPE tab_varchar2 AS TABLE OF VARCHAR2(4000);
  • -- 新建 concat_array 函数
  • CREATE OR REPLACE FUNCTION concat_array(p tab_varchar2) RETURN CLOB IS
  • l_result CLOB;
  •     BEGIN
  •         FOR cc IN (SELECT column_value FROM TABLE(p) ORDER BY column_value) LOOP
  •             l_result := l_result ||' '|| cc.column_value;
  •         END LOOP;
  •         return l_result;
  •     END;

以上函数直接执行就可以了

然后在进行查询返回的就是CLOB字段

方法二:You can use xmlagg + xmlparse grammar to solve ,

Guess you like

Origin blog.csdn.net/qq_39008613/article/details/103352050