Home » SQL & PL/SQL » SQL & PL/SQL » String longer than 4000 chars inside cursor-for loop (Oracle 11g, Oracle SQL Developer 3.2.10.09)
String longer than 4000 chars inside cursor-for loop [message #642421] Thu, 10 September 2015 06:59 Go to next message
yepp
Messages: 4
Registered: September 2015
Junior Member
Hello,

after creating a procedure that reads SQL statements out of a table and executes them, I got in trouble with longer statements, e.g. more than 6000 characters.

I reduced the code while trying to find out the reason and ended up with these few lines of code, which cause "ORA-01460 unimplemented or unreasonable conversion requested" as soon as l_sql will be longer than 4000 characters:

DECLARE

l_sql VARCHAR2(32767);

BEGIN

l_sql := rpad('X', 4001, 'X');

FOR rec IN (SELECT l_sql FROM dual)
  LOOP
    NULL;
  END LOOP;
END;


Any idea how to avoid the error and make it work with strings longer than 4000 characters?

Thank you in advance. Smile

[Updated on: Thu, 10 September 2015 07:14]

Report message to a moderator

Re: String longer than 4000 chars inside cursor-for loop [message #642422 is a reply to message #642421] Thu, 10 September 2015 07:33 Go to previous messageGo to next message
cookiemonster
Messages: 13966
Registered: September 2008
Location: Rainy Manchester
Senior Member
The size limit of varchar in SQL is 4000 characters.
You want something longer then you need to use a clob instead.
Re: String longer than 4000 chars inside cursor-for loop [message #642424 is a reply to message #642422] Thu, 10 September 2015 07:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68757
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

As cookiemonster said:
SQL> DECLARE
  2
  3  l_sql VARCHAR2(32767);
  4
  5  BEGIN
  6
  7  l_sql := rpad('X', 4001, 'X');
  8
  9  FOR rec IN (SELECT l_sql FROM dual)
 10    LOOP
 11      NULL;
 12    END LOOP;
 13  END;
 14  /
DECLARE
*
ERROR at line 1:
ORA-01460: unimplemented or unreasonable conversion requested
ORA-06512: at line 9

SQL> 3
  3* l_sql VARCHAR2(32767);
SQL> c:VARCHAR2(32767):clob
  3* l_sql clob;
SQL> l
  1  DECLARE
  2
  3  l_sql clob;
  4
  5  BEGIN
  6
  7  l_sql := rpad('X', 4001, 'X');
  8
  9  FOR rec IN (SELECT l_sql FROM dual)
 10    LOOP
 11      NULL;
 12    END LOOP;
 13* END;
SQL> /

PL/SQL procedure successfully completed.

Re: String longer than 4000 chars inside cursor-for loop [message #642427 is a reply to message #642422] Thu, 10 September 2015 08:26 Go to previous messageGo to next message
yepp
Messages: 4
Registered: September 2015
Junior Member
cookiemonster wrote on Thu, 10 September 2015 07:33
The size limit of varchar in SQL is 4000 characters.

But my code is an anonymous PLSQL block. I thought, inside PLSQL a VARCHAR2 variable can contain up to 32767 characters.

For other reasons (I need to use the translate function, which works not with LOBs), I tried to avoid LOBs.
Re: String longer than 4000 chars inside cursor-for loop [message #642428 is a reply to message #642427] Thu, 10 September 2015 08:40 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
yepp wrote on Thu, 10 September 2015 06:26
cookiemonster wrote on Thu, 10 September 2015 07:33
The size limit of varchar in SQL is 4000 characters.

But my code is an anonymous PLSQL block. I thought, inside PLSQL a VARCHAR2 variable can contain up to 32767 characters.

For other reasons (I need to use the translate function, which works not with LOBs), I tried to avoid LOBs.

SQL is SQL
PL/SQL is PL/SQL
They are DIFFERENT
Just because a SQL statement resides inside PL/SQL block, it does not change the rules for the SQL execution.
Accept this reality.
Re: String longer than 4000 chars inside cursor-for loop [message #642429 is a reply to message #642428] Thu, 10 September 2015 08:56 Go to previous messageGo to next message
yepp
Messages: 4
Registered: September 2015
Junior Member
BlackSwan wrote on Thu, 10 September 2015 08:40
Accept this reality.

I've no problem to accept something.
I try to understand, why this code is not running.

Is it, because the part "SELECT l_sql FROM dual"? A SELECT can never give back more than 4000 characters?


So, I've to swap the whole solution to CLOB and then replace the translate() function (with a couple of characters as parameter) by a couple of replace() calls.
Re: String longer than 4000 chars inside cursor-for loop [message #642430 is a reply to message #642429] Thu, 10 September 2015 09:21 Go to previous messageGo to next message
cookiemonster
Messages: 13966
Registered: September 2008
Location: Rainy Manchester
Senior Member
A select statement is SQL, regardless of where it's run from. Likewise insert, update, delete and merge.
In SQL varchar2 is limited to 4000.
But of course you can get longer strings back, you just have to use the correct datatype as Michel showed.
Re: String longer than 4000 chars inside cursor-for loop [message #642435 is a reply to message #642430] Thu, 10 September 2015 11:58 Go to previous messageGo to next message
jois
Messages: 12
Registered: October 2014
Junior Member
Thanks Michel for the example.

https://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:6141473100346333010

In the above link Tom details as as below.

In plsql, the limit for strings was 32k (and if you use clobs - virtually unlimited, as is the database type clob)
all I can say is that in 11g and below, the limit is definitely 4000 bytes whereas plsql is 32k. And, this mismatch will go away (future tense) - soon.

So,what is he referring 32k variable in plsql? where can we actually use this in plsql. I could not follow.

Thanks everyone.

[Updated on: Thu, 10 September 2015 12:20]

Report message to a moderator

Re: String longer than 4000 chars inside cursor-for loop [message #642437 is a reply to message #642421] Thu, 10 September 2015 13:44 Go to previous messageGo to next message
John Watson
Messages: 8974
Registered: January 2010
Location: Global Village
Senior Member
Do you have the option to upgrade to release 12.x? It gives you the ability to create SQL varchars up to 32K, which gives you compatibility with the PL/SQL. Syntactically, you can use them like any other varchar though the implementation is in fact using LOBs. I recorded a simple demo here,

32KB varchar2 columns



Re: String longer than 4000 chars inside cursor-for loop [message #642452 is a reply to message #642435] Fri, 11 September 2015 02:44 Go to previous messageGo to next message
cookiemonster
Messages: 13966
Registered: September 2008
Location: Rainy Manchester
Senior Member
Jay1 wrote on Thu, 10 September 2015 17:58

So,what is he referring 32k variable in plsql? where can we actually use this in plsql. I could not follow.



Means what it says - varchar2 variables in PL/SQL can be 32767. You can use them anywhere in PL/SQL apart from in SQL.
Re: String longer than 4000 chars inside cursor-for loop [message #642486 is a reply to message #642452] Fri, 11 September 2015 11:12 Go to previous message
jois
Messages: 12
Registered: October 2014
Junior Member
Thanks John and cookiemonster for details.
Previous Topic: Count records from two tables
Next Topic: how to write this unpivot query
Goto Forum:
  


Current Time: Sun Apr 27 01:27:16 CDT 2025