84

When I try to query objects, I end up with following error:

ORA-01461: can bind a LONG value only for insert into a LONG column

Could someone please help me on the cause and solution of the problem?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Rupesh
  • 983
  • 3
  • 11
  • 20
  • @Rupesh are you sure this is from a query? Are you trying to do an insert maybe? Also, how is this run (java, C#, sqlplus, ?) – tbone Feb 06 '12 at 11:39
  • 1
    PL/SQL workarounds exist for using long string literals to insert into [CLOB](http://stackoverflow.com/questions/8801814/how-to-insert-update-larger-size-of-data-in-the-oracle-tables) and [BLOB](http://stackoverflow.com/questions/18116634/oracle-10-using-hextoraw-to-fill-in-blob-data) columns. – Vadzim May 27 '16 at 15:37
  • For most people seeking a solution, this one would be the right one (not the accepted one): https://stackoverflow.com/a/14497831/1657465 – luiscla27 Apr 13 '21 at 19:04

17 Answers17

217

It can also happen with varchar2 columns. This is pretty reproducible with PreparedStatements through JDBC by simply

  1. creating a table with a column of varchar2 (20 or any arbitrary length) and
  2. inserting into the above table with a row containing more than 20 characters

So as above said it can be wrong with types, or column width exceeded.

Also note that as varchar2 allows 4k chars max, the real limit will be 2k for double byte chars.

starball
  • 20,030
  • 7
  • 43
  • 238
Kiran
  • 3,151
  • 2
  • 20
  • 16
  • 17
    In an Oracle db I have a table with a column that is VARCHAR2(2000 CHAR). I see both the ORA-01461 (about that long value) and another error ORA-12899. The ORA-12899 precisely states the problem: actual length: 2392, max length: 2000. It seems that the kind of misleading ORA-01461 is returned when content > 4000 CHAR, which is the maximum supported by Oracle. While the ORA-12899 is returned when the length is > 2000 and < 4000. Can anyone confirm this (for Oracle at least)? – Vering Oct 14 '14 at 08:34
  • 6
    @Vering, can confirm. We get the same behavior. – Grant H. Jan 02 '15 at 14:57
  • 2
    This is what was really happening for us. – Mathew Berg Aug 09 '17 at 12:14
  • I had the ORA-01461 for a column of VARCHAR2(2000). – Marcel Jan 09 '18 at 15:40
  • 1
    This helped, thank you. Had VARCHAR2(512 CHAR) and value was 1500 characters – Alexander Murza Mar 27 '18 at 10:43
  • 1
    This was the actual error that was happening for me as well. Thank you! :) – Niklas S. May 21 '19 at 08:58
  • 1
    Out of curiosity, why does Oracle produce such an arcane error in this case? – Seldon Sep 24 '19 at 11:14
38

This error occurs when one attempts to use a varchar variable longer than 4000 bytes in an SQL statement. PL/SQL allows varchars up to 32767 bytes, but the limit for database tables and SQL language is 4000. You can't use PL/SQL variables that SQL doesn't recognize in SQL statements; an exception, as the message explains, is a direct insert into a long-type column.

create table test (v varchar2(10), c clob);


declare
  shortStr varchar2(10) := '0123456789';
  longStr1 varchar2(10000) := shortStr;
  longStr2 varchar2(10000);
begin
  for i in 1 .. 10000
  loop
    longStr2 := longStr2 || 'X';
  end loop;

  -- The following results in ORA-01461
  insert into test(v, c) values(longStr2, longStr2);

  -- This is OK; the actual length matters, not the declared one
  insert into test(v, c) values(longStr1, longStr1);

  -- This works, too (a direct insert into a clob column)
  insert into test(v, c) values(shortStr, longStr2);

  -- ORA-01461 again: You can't use longStr2 in an SQL function!
  insert into test(v, c) values(shortStr, substr(longStr2, 1, 4000));
end;
Tomasz Żuk
  • 1,288
  • 1
  • 12
  • 14
18

Ok, well, since you didn't show any code, I'll make a few assumptions here.

Based on the ORA-1461 error, it seems that you've specified a LONG datatype in a select statement? And you're trying to bind it to an output variable? Is that right? The error is pretty straight forward. You can only bind a LONG value for insert into LONG column.

Not sure what else to say. The error is fairly self-explanatory.

In general, it's a good idea to move away from LONG datatype to a CLOB. CLOBs are much better supported, and LONG datatypes really are only there for backward compatibility.

Here's a list of LONG datatype restrictions

Hope that helps.

Mark J. Bobak
  • 13,720
  • 6
  • 39
  • 67
  • 17
    The error can occur without perceivably operating on a table with a LONG type. – Gus Crawford Mar 17 '15 at 16:45
  • 3
    I got it when trying to merge some data into a table with a CLOB – dougd_in_nc Mar 11 '16 at 17:30
  • 23
    Unfortunately, the error message is not very self explanatory, if it is caused by a varchar2 field which exceeds its field length (see answer of Kiran) – Habizzle Oct 10 '16 at 08:59
  • 3
    I failed to scroll down and realized that Kiran had the better answer. I don't like the idea of how the accepted answer is at the top, there should be some way to bump it to second when another answer gets way more upvotes. – CoderSteve Dec 05 '16 at 18:55
  • 3
    it happens even if you try to put more characters than the size you specify for the varchar column. for example varchar(4000): if you give 4001 characters you get that error. – Laura Liparulo Sep 18 '17 at 08:54
  • 1
    Not so much self-explanatory, as others have stated already. I find that the answer below is much more useful. – Niklas S. May 21 '19 at 08:58
  • Not at all self-explanatory. Failing in our app due to varchar length exceeded. – David Victor Sep 29 '20 at 11:56
15

This ORA-01461 does not occur only while inserting into a Long column. This error can occur when binding a long string for insert into a VARCHAR2 column and most commonly occurs when there is a multi byte(means single char can take more than one byte space in oracle) character conversion issue.

If the database is UTF-8 then, because of the fact that each character can take up to 3 bytes, conversion of 3 applied to check and so actually limited to use 1333 characters to insert into varchar2(4000).

Another solution would be change the datatype from varchar2(4000) to CLOB.

Murali
  • 289
  • 3
  • 8
9

A collegue of me and I found out the following:

When we use the Microsoft .NET Oracle driver to connect to an oracle Database (System.Data.OracleClient.OracleConnection)

And we are trying to insert a string with a length between 2000 and 4000 characters into an CLOB or NCLOB field using a database-parameter

oraCommand.CommandText = "INSERT INTO MY_TABLE (NCLOB_COLUMN) VALUES (:PARAMETER1)";
// Add string-parameters with different lengths
// oraCommand.Parameters.Add("PARAMETER1", new string(' ', 1900)); // ok
oraCommand.Parameters.Add("PARAMETER1", new string(' ', 2500));  // Exception
//oraCommand.Parameters.Add("PARAMETER1", new string(' ', 4100)); // ok
oraCommand.ExecuteNonQuery();
  • any string with a length under 2000 characters will not throw this exception
  • any string with a length of more than 4000 characters will not throw this exception
  • only strings with a length between 2000 and 4000 characters will throw this exception

We opened a ticket at microsoft for this bug many years ago, but it has still not been fixed.

Markus1980Wien
  • 471
  • 1
  • 5
  • 15
  • We have the same issue. Do you have a reasonable workaround? – jeromerg Jan 18 '16 at 14:58
  • if possible use Oracle.ManagedDataAccess.dll. (can be deployed freely, no need to install an Oracle-Client on Client-Computers). [requires.NET 4.0] If you cannot change to other .NET DataProvider there are some possible workarounds I can image: We stored XML-Data or human readable text in such fields. So we appended spaces (if length of string was between 2000 und 4000 characters.) other possible solution would be to save string length in an extra database-field, append spaces if string-length is between 2000 und 4000, and use the length information in the extra field, to cut string on read. – Markus1980Wien Jan 25 '16 at 16:47
4

I was facing the same issue and solve it by just replacing VARCHAR with CLOB. This link helped me out.

aneela
  • 1,457
  • 3
  • 24
  • 45
3

Kiran's answer is definetely the answer for my case.

In code part I split string to 4000 char strings and try to put them in to db.

Explodes with this error.

The cause of the error is using utf chars, those counts 2 bytes each. Even I truncate to 4000 chars in code(sth. like String.Take(4000)), oracle considers 4001 when string contains 'ö' or any other non-eng(non ascii to be precise, which are represented with two or bytes in utf8) characters.

mkb
  • 1,106
  • 1
  • 18
  • 21
3

Applications using JDBC 10.1 has got a bug (Doc ID 370438.1) and can throw the same ORA-01461 exception while working with UTF8 character set database even though inserted characters are less than the maximum size of the column.

Recommended Solution: - Use 10gR2 JDBC drivers or higher in such case.

HTH

pahariayogi
  • 1,073
  • 1
  • 7
  • 18
2

I know it is a quite old question but I had exactly the same error and it was a stupid mistake from my side.

I simply tried to insert a byte array (a pdf file) into a CLOB, changing the type to a BLOB fixed the issue.

I spent some time before I realized my mistake without finding any good lead (and the error message is not really obvious), then maybe my answer will help someone else.

893
  • 185
  • 1
  • 5
  • 14
1

I had the same problem with Entity Framework database first on all CLOB columns.

As a workaround, I filled the text values with spaces to be at least 4000 in width in insert operations (did not come with any better solution).

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
1

I encountered this error message when trying to insert String into an XMLTYPE column.

Specifically using Java's PreparedStatement like this:

ps.setString('XML', document);

where XML here is defined as XMLTYPE.

doughgle
  • 827
  • 1
  • 9
  • 18
1

I had the same problem using PHP and prepared statements on a VARCHAR2 column. My string didn't exceeed the VARCHAR2 size. The problem was that I used -1 as maxlength for binding, but the variable content changed later.

In example:

$sMyVariable = '';
$rParsedQuery = oci_parse($rLink, 'INSERT INTO MyTable (MyVarChar2Column) VALUES (:MYPLACEHOLDER)');
oci_bind_by_name($rParsedQuery, ':MYPLACEHOLDER', $sMyVariable, -1, SQLT_CHR);

$sMyVariable = 'a';
oci_execute($rParsedQuery, OCI_DEFAULT);
$sMyVariable = 'b';
oci_execute($rParsedQuery, OCI_DEFAULT);

If you replace the -1 with the max column width (i. e. 254) then this code works. With -1 oci_bind_by_param uses the current length of the variable content (in my case 0) as maximum length for this column. This results in ORA-01461 when executing.

David Gausmann
  • 1,570
  • 16
  • 20
1

In my particular case, I was trying to store a Base64 encoded file into a table BLOB field, using Mybatis.

So in my xml I had:

<insert id="save..." parameterType="...DTO">
    <selectKey keyProperty="id" resultType="long" order="BEFORE">
        SELECT SEQ.nextVal FROM DUAL
    </selectKey>
    insert into MYTABLE(
        ID,
        ...,
        PDF
    ) values (
        #{id, jdbcType=VARCHAR},
        ...,
        #{tcPdf, jdbcType=BLOB},
    )
</insert>

and in my DTO:

String getPdf(){
    return pdf;
}

That makes to Mybatis threat as if were a String char sequence and try to store it as a Varchar. So my solution was the following:

In my DTO:

Byte[] getPdf(){
    return pdf.getBytes();
}

And worked.

I hope this could help anybody.

Francisco M
  • 163
  • 2
  • 9
0

I encountered the same problem using Siebel REXPIMP (registry import) when using the latest Instant Client driver. To fix the issues, use the Siebel provided Data Direct driver instead. The DLL is SEOR823.DLL

cmd
  • 11,622
  • 7
  • 51
  • 61
0

Adding another use case where I found this happening. I was using a ADF Fusion application and the column type being used was a varchar2(4000) which could not accommodate the text and hence this error.

Vik
  • 8,721
  • 27
  • 83
  • 168
0

I have a solution for Java/JPA/eclipselink/oracle when insert a long xml string (>4000) into a XMLTYPE column at Insert XML with more than 4000 characters into a Oracle XMLTYPE column. For clarity, include the same contents here in case the link not working

You need to convert xml string for more than 4000 charcaters into SQLXML type first.

Environment: jpa 2.1.0, eclipselink 2.5.2, oracle db 11gr2

SQL:

CREATE TABLE "XMLTEST"
( "ID" NUMBER(10,0) NOT NULL ENABLE, 
  "DESCRIPTION" VARCHAR2(50 CHAR) NOT NULL ENABLE, 
  "XML_TXT" "XMLTYPE" NOT NULL ENABLE
);

INSERT INTO XMLTEST (ID, DESCRIPTION, XML_TXT) VALUES (101, 'XML DATA', '<data>TEST</data>');
COMMIT;

DROP TABLE "XMLTEST";

Java Code

String sql = "INSERT INTO XMLTEST (ID, DESCRIPTION, XML_TXT) VALUES (?, ?, ?)";
String xmlDataStr = "<data>test...</data>"; // a long xml string with length > 4000 characters
Connection con = getEntityManager().unwrap(Connection.class);
SQLXML sqlXml = con.createSQLXML();
sqlXml.setString(xmlDataStr);

Java code - use PreparedStatement

PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setLong(1, 201);
pstmt.setLong(2, "Long XML Data");
pstmt.setSQLXML(3, sqlXml);
pstmt.execute();

Java code - use native query instead of PreparedStatement

Query query = getEntityManager().createNativeQuery(sql);
query.setParameter(1, 301);
query.setParameter(2, "Long XML Data");
query.setParameter(3, sqlXml);
query.executeUpdate();
Community
  • 1
  • 1
Jonathan L
  • 9,552
  • 4
  • 49
  • 38
0

I resolved the error I was facing by changing my column from VARCHAR2(4000) to NVARCHAR2(2000). The issue was due to special characters, which NVARCHAR2 can handle. While CLOB could also be a solution, it's more suited for very long text. If you're dealing with text under 2000 characters that includes special characters, NVARCHAR2 is a practical choice.