CLOB type processing resolution in Oracle: ORA-01461: Only long values can be inserted into long column assignments

Source: Internet
Author: User

The analysis of CLOB type processing in the original Broken Sword ice-oracle

Previous analysis: ORA-01461: Can only bind a long value to insert a long column

Most recently, for the Clob field, when inserting data, the number of bytes in the character (one half-width character, one byte, one full-width character, two bytes) between 2000-4000 times is wrong (ORA-01461: You can only insert a Long value assignment for a long column). After constantly looking for information and their own test the problem finally solved, below I will own experience to do a share.

Get ready

System Environment xp+.net2.0+oracle9i

Table structure (because it is a test, the table structure randomly built a) XX

Field name Type
Id VARCHAR2 (70)
TEST Clob

Test

Way 1 : Direct the CLOB the value is spelled in the SQL statement.

Code:

string id =="insert INTO XX (id,test) VALUES ('""" , '"')"; // data is a variable that stores the string cmd you want to insert . ExecuteNonQuery ();

Situation Analysis :

When the length of the data is greater than 4000 times the error (ORA-01704: The literal string is too long), the normal insertion is less than or equal to 4000.

Cause Analysis:

The reason for the fact that the length is greater than 4000 times is wrong because there are no more than 4000 characters between the two single quotes in the SQL statement in Oracle. ' + data + ' data in the SQL statement, when the value of data is greater than 4,000 bytes will be an error.

Workaround:

This approach is tricky, but there are better ways to do it below.

Way 2 : Takes the form of a parameter.

Code:

  Guid.NewGuid (). ToString (); OracleCommand cmd  = Conn.createcommand (); cmd. CommandText  =  insert into XX (id,test) VALUES ('   '  + ID +  '    " ;oracleparameter p1  = Span style= "color: #0000ff;" >new  OracleParameter ( p1   "  = data; //  data is a variable that stores the string you want to insert  cmd. Parameters.Add (p1); cmd. ExecuteNonQuery ();  

Situation Analysis :

This way you can insert normally. So recommended in this way.

Cause Analysis:

No

Workaround:

No

Way 3 : Takes a Parameter form, but the parameter type is written as OracleType. NVarChar

Code:

  Guid.NewGuid (). ToString (); OracleCommand cmd  = Conn.createcommand (); cmd. CommandText  =  insert into XX (id,test) VALUES ('   '  + ID +  '    " ;oracleparameter p1  = Span style= "color: #0000ff;" >new  OracleParameter ( p1   "  = data; //  data is a variable that stores the string you want to insert  cmd. Parameters.Add (p1); cmd. ExecuteNonQuery ();  

Situation Analysis :

Why write this way, because this way is very similar to the way to adopt NHibernate, first see what happens in this way. When the number of bytes of data between 0-2000 normal insertion, more than 4000 when the normal insertion, but at 2000-4000 failed, error (ORA-01461: can only be inserted long column Long value assignment)

Cause Analysis:

The corresponding Oracle type is not used.

Workaround:

With Oracletype.clob

Related Article

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.