DBA Tips Archive for Oracle

  


Understanding Maximum Sizes for VARCHAR2 Data

by Jeff Hunter, Sr. Database Administrator

Overview

Oracle provides the ability in both SQL and PL/SQL to work with the VARCHAR2 data type. However, they differ in their maximum allowed length. Within PL/SQL, local variables of type VARCHAR2 can be declared to contain up to 32,767 characters while declaring a database column of type VARCHAR2 can contain up to 4000 characters.

This SQL VARCHAR2 limit of 4000 characters exists even when VARCHAR2 values appear inside DML statements like SELECT, INSERT, UPDATE and DELETE.

When referencing PL/SQL functions inside DML statements, ensure the return value does not exceed 4000 characters.

Example

The example below illustrates the problem when referencing PL/SQL functions inside DML statements:
  CREATE OR REPLACE FUNCTION starcount(number_of_stars NUMBER)
  RETURN VARCHAR2 IS
  BEGIN
    RETURN RPAD('*',number_of_stars,'*');
  END;
  /

The statement:

  select starcount(4000) from dual;
would select and display 4000 stars while the statement:
  select starcount(4001) from dual;
would produce the following error:
  select starcount(4001) from dual
         *
  ERROR at line 1:
  ORA-06502: PL/SQL: numeric or value error: character string buffer too small
  ORA-06512: at line 1

If you require to work with character data greater than 4K, use the CLOB data type and the DBMS_LOB package.



Copyright (c) 1998-2017 Jeffrey M. Hunter. All rights reserved.

All articles, scripts and material located at the Internet address of http://www.idevelopment.info is the copyright of Jeffrey M. Hunter and is protected under copyright laws of the United States. This document may not be hosted on any other site without my express, prior, written permission. Application to host any of the material elsewhere can be made by contacting me at jhunter@idevelopment.info.

I have made every effort and taken great care in making sure that the material included on my web site is technically accurate, but I disclaim any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on it. I will in no case be liable for any monetary damages arising from such loss, damage or destruction.

Last modified on
Sunday, 21-Jul-2002 00:00:00 EDT
Page Count: 80977