1
votes

I use Oracle 12c. I have below table in my DB.

CREATE TABLE TEST_T (COL VARCHAR2(4000 CHAR));

I need insert multibyte characters in this table. The character is 3 byte character.
I am able to insert only 1333 (upto 3999 bytes) characters in table. My expectation is to insert upto 1500 multibyte characters but I get ORA - 01461.
I don't want to change data type to CLOB or LONG. Is there any way to use VARCHAR2(4000 CHAR) to achieve this.

Below is the code,

SET SERVEROUTPUT ON
DECLARE
  LV_VAR CHAR(1):='プ';     -- 3 byte character
  LV_STR VARCHAR2(32000) := '';
BEGIN
  FOR I IN 1..1500
  LOOP
    LV_STR := LV_STR||LV_VAR;
  END LOOP;
--
  INSERT INTO TEST_T VALUES (LV_STR);
END;
/



Error report -
ORA-01461: can bind a LONG value only for insert into a LONG column
ORA-06512: at line 11
01461. 00000 -  "can bind a LONG value only for insert into a LONG column"
*Cause:    
*Action:
3
What is max_string_size set to? Could you set that to extended rather than the default of standard? If so, that would allow varchar2 columns 32k long rather than 4000 bytes.Justin Cave
@JustinCave It is set to standard only however I will not be able to change it to extended due to restrictions.hemalp108

3 Answers

3
votes

The problem is that the 4000 byte limit is a hard limit, regardless of whether the datatype is defined as VARCHAR2(4000 CHAR), VARCHAR2(4000 BYTE), or NVARCHAR2(4000). This means that multibyte characters will always have the chance of overflowing a max-size non-CLOB text column.

Oracle's table of Datatype Limits shows each of the VARCHAR2 variants as holding a max of 4000 bytes. And this is precisely the problem you have encountered.

You do have the option of increasing the max size for VARCHAR2 in your Oracle 12c database to 32k.

Here's how to do it: MAX_STRING_SIZE documentation

This is not something to be done without careful consideration: once you change your database to use extended VARCHAR2 strings you cannot go back. Nevertheless, if your database is all your own and you like the idea of having 32K strings, then this feature was created specifically to address your situation.

Be careful to read the details of pluggable databases, container databases as they require different upgrade techniques. This is a change that cuts across the entire database so you want to get it right.

1
votes

Use NVARCHAR2 instead of VARCHAR2

NCHAR and NVARCHAR2 are Unicode datatypes that store Unicode character data. The character set of NCHAR and NVARCHAR2 datatypes can only be either AL16UTF16 or UTF8 and is specified at database creation time as the national character set. AL16UTF16 and UTF8 are both Unicode encoding.

The maximum length of an NVARCHAR2 column is 4000 bytes. It can hold up to 4000 characters. The actual data is subject to the maximum byte limit of 4000. The two size constraints must be satisfied simultaneously at run time.

1
votes

The maximum size for VARCHAR2 is 4000 bytes (VARCHAR2 max size) and is not 4000+ bytes for multibyte characters. You have to change the type to CLOB or NVARCHAR2.

The maximum byte length of an NVARCHAR2 depends on the configured national character set (NVARCHAR2).