The maximum length of varchar2 in oracle

Source: Internet
Author: User
Varchar2 has two maximum lengths: one is in field type 4000, and the other is in PLSQL variable type 32767. 1. The return length of the varchar2 type of the function is also 4000, rather than 32767. This is a relatively error-prone place. This is because the function can declare a string variable with a length greater than 4000 and return it.

Varchar2 has two maximum lengths: one is in field type 4000, and the other is in PL/SQL variable type 32767. 1. The return length of the varchar2 type of the function is also 4000, rather than 32767. This is a relatively error-prone place. This is because the function can declare a string variable with a length greater than 4000 and return it.

Varchar2 has two maximum lengths: one is in field type 4000, and the other is in PL/SQL variable type 32767.


1. The varchar2 type return value length of the function is also 4000, rather than 32767.

This is a relatively error-prone place. Because the function can declare a string variable with a length greater than 4000 and use it as the return value, compilation errors are not prompted here. This function can be executed normally, and once the length of this string exceeds 4000, the function execution will fail.

For example:

SQL> create or replace function funny return varchar2
2 is
3 v_yct varchar2 (32767 );
4 begin
5 v_yct: = rpad ('a, 4001, 'B ');
6 return v_yct;
7 end;
8/

The function has been created.

SQL> select funny from dual;
Select funny from dual
*
Row 3 has an error:
ORA-06502: PL/SQL: Number or value error: string buffer is too small
ORA-06512: In "TEST. FUNNY", line 6


2. It may be that the schema-level varchar2 length limit is 4000, while the PL/SQL code-level length limit is 32767.

Continue the test below:

(1) As a function input parameter, the limit is 32767
SQL> create or replace function funny (p_char in varchar2) return number
2 is
3 begin
4 return length (p_char );
5 end;
6/

The function has been created.

SQL> declare
2 v_char varchar2 (32767 );
3 begin
4 v_char: = rpad ('A', 32767, 'B ');
5 dbms_output.put_line (funny (v_char ));
6 end;
7/
32767

The PL/SQL process is successfully completed.

(2) For the same function, the length limit at the schema level is different from that at the PL/SQL code level.
SQL> select length (rpad ('A', 4001, 'B') from dual;

LENGTH (RPAD ('A', 3276558, 'B '))
-----------------------------
4000

SQL> begin
2 dbms_output.put_line (length (rpad ('A', 4001, 'B ')));
3 end;
4/
4001

The PL/SQL process is successfully completed.

3. What if the length of the intermediate result string exceeds the limit?

(1) In PL/SQL, if the intermediate result exceeds 32767, there is no impact:

SQL> declare
2 v_char1 varchar2 (32767 );
3 v_char2 varchar2 (32767 );
4 v_char3 varchar2 (10 );
5 begin
6 v_char1: = lpad ('A', 32767, 'A ');
7 v_char2: = lpad ('B', 32767, 'B ');
8 v_char3: = substr (v_char1 | v_char2, 32763, 10 );
9 dbms_output.put_line (v_char3 );
10 end;
11/
Aaaaabbbbb

The PL/SQL process is successfully completed.

(2) If the intermediate results at the schema level exceed 4000, an error is prompted:

SQL> select substr (rpad ('A', 4000, 'A') | rpad ('B', 4000, 'B'), 3996, 10) from dual;
Select substr (rpad ('A', 4000, 'A') | rpad ('B', 4000, 'B'), 3996, 10) from dual
*
Row 3 has an error:
ORA-01489: the result of string connection is too long

4. What should I do if the length of a string exceeds the limit? The CLOB type can be used.

For example, in the first example, you can change the return value to the CLOB type:

SQL> create or replace function funny return clob
2 is
3 v_yct varchar2 (32767 );
4 begin
5 v_yct: = rpad ('a, 4001, 'B ');
6 return v_yct;
7 end;
8/

The function has been created.

SQL> select funny from dual;

FUNNY
--------------------------------------------------------------------------------
Bytes

SQL> set long 10000
SQL> select funny from dual;
FUNNY
--------------------------------------------------------------------------------
Bytes
Bytes
Bytes
Bytes
.
In this way, the maximum length of the returned string is 32767.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.