티스토리 뷰

ORA-64203: 문자 집합 변환 후 대상 버퍼가 너무 작아 CLOB 데이터를 보유할 수 없습니다.

시스템 운영 중 위와 같은 오류가 발생하였습니다.
Oracle 공식 문서를 확인 해 보니, 아래와 같이 설명이 되어 있습니다.

ORA-64203: Destination buffer too small to hold CLOB data after character set conversion.
Cause: An attempt was made to convert CLOB to CHAR, where the LOB size was bigger than the buffer limit for CHAR types or the CHAR buffer was not big enough to hold all data after character set conversion.
Action: Do one of the following:

* Make the LOB smaller before performing the conversion. for example, by using SUBSTR on CLOB


* Use DBMS_LOB.SUBSTR to convert CLOB to CHAR.

즉, 쿼리 상에서 LOB 타입의 컬럼을 TO_CHAR 를 통해 VARCHAR2 형태로 변환시 버퍼 사이즈(4000Byte)가 부족해 발생한 문제였습니다.
해결책은 SUBSTR 을 이용하여 TO_CHAR 전에 사이즈를 줄여주어야 합니다.

예를 들어 ITEMS 테이블에서 ITEM설명 컬럼이 CLOB 형이고, 데이터 길이가 4000Byte 가 넘어가는 경우

SELECT TO_CHAR(ITEM_DESC)
FROM ITEMS
SELECT TO_CHAR(SUBSTR(ITEM_DESC, 1, 4000))
FROM ITEMS

SUBSTR 을 통해 적정 Byte 만큼 잘라주어야 합니다. 참고로 Oracle 에서 LOB 데이터는 SUBSTRB 같은 함수는 지원하지 않습니다.
만약 수행해보면 “ORA-22998: CLOB or NCLOB in multibyte character set not supported” 라는 오류가 발생합니다.
따라서 한글을 포함하고 있다면 적정한 글자수로 잘라 주어야 합니다.

그리고 구글링을 하다보니 아래와 같은 Comment 를 접하게 되었습니다.

If you have a CLOB greater than 4000 bytes, you need to use DBMS_LOB.SUBSTR rather than SUBSTR. Note that the amount and offset parameters are reversed in DBMS_LOB.SUBSTR

Oracle 공식문서를 통해서 확인해 보면,

Starting with Oracle 9i, many SQL functions can be used for manipulating CLOBs directly instead of using the DBMS_LOB package. So can f.i. the DBMS_LOB.SUBSTR function be replaced by the SQL function SUBSTR. However, unlike other functions in the DBMS_LOB package, DBMS_LOB.SUBSTR cannot be used interchangeably with the SQL function SUBSTR because the order of the position and length parameters are switched. To replace one function with the other, you have to re-code the position and length parameters in DBMS_LOB.SUBSTR(lob_locator, length, position) to SUBSTR(string, position, length) and vice versa.

즉, DBMS_LOB.SUBSTR 을 사용해도 상관없지만, 함께 사용하거나 변환할 땐 조심해야 한다고 되어 있습니다.

SUBSTR 을 통해 CLOB 데이터를 VARCHAR2 형태로 변환이 가능하지만, DB 에서는 이 또한 권장하지 않는다고 합니다.
CLOB 을 DB 상에서 핸들링(SUBSTR 같은...) 하게되면 Temp Segment 영역을 많이 차지하게 되고 Release 하지 않고 쌓이는 경향이 있다고 합니다.(by DBA)
따라서 위와 같은 케이스의 경우 CLOB 형태로 그대로 select 를 한 뒤 프로그램 단에서 잘라서 사용하길 권장합니다.(필요시 자르기)




추가적으로 TO_CHAR 시 버퍼사이즈가 4000Byte 로 제한이 되어 있는데, 이는 DB 설정으로도 변경이 불가능한 항목입니다.
아마도 VARCHAR2 최대 사이즈가 4000Byte 라서 그런 것으로 생각됩니다.
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함