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

前端 未结 15 1755
野的像风
野的像风 2020-11-30 02:44

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 someo

相关标签:
15条回答
  • 2020-11-30 03:24

    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.

    0 讨论(0)
  • 2020-11-30 03:26

    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.

    0 讨论(0)
  • 2020-11-30 03:27

    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();
    
    0 讨论(0)
  • 2020-11-30 03:28

    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.

    0 讨论(0)
  • 2020-11-30 03:30

    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

    0 讨论(0)
  • 2020-11-30 03:32

    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.

    0 讨论(0)
提交回复
热议问题