Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Sql Developer "string literal too long" error

Tags:

sql

oracle

I have the following SQL that I would like to run in Oracle SQL Developer against an Oracle 10g server:

WITH openedXml AS (
  SELECT extractvalue(column_value, '/theRow/First') FIRST,
         extractvalue(column_value, '/theRow/Last') LAST,
         to_number(extractvalue(column_value, '/theRow/Age')) Age
    FROM TABLE(XMLSequence(XMLTYPE('
  <theRange>
    <theRow><First>Bob</First><Last>Smith</Last><Age>30</Age></theRow>
    <theRow><First>Sue</First><Last>Jones</Last><Age>34</Age></theRow>
...
...
...
    <theRow><First>Tom</First><Last>Anderson</Last><Age>39</Age></theRow>
    <theRow><First>Ali</First><Last>Grady</Last><Age>45</Age></theRow>
  </theRange>
  ').extract('/theRange/theRow')))
)
SELECT *
FROM openedxml
WHERE age BETWEEN 30 AND 35;

When I attempt to run it I get the following error:

Error at Command Line:1 Column:0 Error report: SQL Error: ORA-01704: string literal too long
01704. 00000 -  "string literal too long"
*Cause:    The string literal is longer than 4000 characters.
*Action:   Use a string literal of at most 4000 characters.
           Longer values may only be entered using bind variables.

My strings will occasionally be much longer than 4000 characters. Any ideas about how I can get around this problem?

like image 340
wcm Avatar asked Mar 30 '11 13:03

wcm


People also ask

How do you resolve ORA 01704 string literal too long?

You can't do it with a literal as part of the sql statement. You'll need to use a bind variable instead, and send the data in chunks, the same as you would if it were LONG datatype, and that code is not specific to Oracle. I need to do the insert using sql script (no bind parameters).

How do I save more than 4000 characters in Oracle?

For strings greater than 4000 use a CLOB. you can use CLOB column.

What is Max length of VARCHAR2 in Oracle?

The maximum length for VARCHAR2 is 32672 BYTE or 8168 CHAR which is the same as the maximum length for VARCHAR of 32672 OCTETS or 8168 CODEUNITS32.


1 Answers

You can't get around this with "plain" SQL. (But I'd be glad to be proven wrong)

You will need some kind of programming language (e.g. Java, Stored Procedure) to deal with this.

An alternative is to upload the XML data into a table (can be done with SQL*Loader) and the use the column values in your query.

This is one of the limitations of Oracle that is really driving me nuts. 20 years ago this might have been somewhat acceptable, but nowadays...

like image 147
a_horse_with_no_name Avatar answered Sep 30 '22 17:09

a_horse_with_no_name