?
Solved

Execute Immediate on a very long string causes exception

Posted on 2008-10-30
5
Medium Priority
?
1,597 Views
Last Modified: 2013-12-19
The SQL is 52,028 characters without spaces and 78,960 with spaces.

I need to use execute immediate so I can define the partition for one of the tables dynamically. This is the only reason that I need the sql to execute in an execute immediate block.

Any thoughts?

ERROR at line 1:
ORA-20000: ORA-06502: PL/SQL: numeric or value error: character string buffer
too small
ORA-06512: at line 1721

Thanks,

Geoff
0
Comment
Question by:gswitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 22842812
execute immediate can only run a varchar2 string which as a 32K limit
0
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 200 total points
ID: 22842822
using dbms_sql you can build longer commands
0
 
LVL 23

Accepted Solution

by:
paquicuba earned 1800 total points
ID: 22842948
/* Source -->  http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/d_sql.htm#sthref6141

To parse SQL statements larger than 32 KB,
DBMS_SQL makes use of PL/SQL tables to pass a table of strings to the PARSE procedure.
These strings are concatenated and then passed on to the Oracle server.

You can declare a local variable as the VARCHAR2S table-item type,
and then use the PARSE procedure to parse a large SQL statement as VARCHAR2S.

The definition of the VARCHAR2S datatype is:
TYPE varchar2s IS TABLE OF VARCHAR2(256) INDEX BY BINARY_INTEGER;

*/

Declare
L_Query_String   CLOB;
L_Buffer         Constant Binary_integer := 256;
L_String_Len          Integer := 0;
L_Start_Pos      Integer := 1;
L_End_Pos        Integer := 256;
L_CLOB_len       Pls_integer;
L_Execution_Val  Pls_integer;
L_Cursor         Pls_integer := Dbms_sql.Open_cursor;
L_Sql_table      Dbms_sql.Varchar2s;

Begin  
 
  L_Query_String := 'Insert Into Test values(1)';

  L_CLOB_len := Dbms_lob.Getlength(L_Query_String);

  Loop

    If L_String_Len + L_Buffer > L_CLOB_len Then
      L_End_Pos := L_CLOB_len - L_String_Len;
    End If;

    L_Sql_table(Nvl(L_Sql_table.Last, 0) + 1) := Dbms_lob.Substr(L_Query_String, L_End_Pos, L_Start_Pos);

    L_Start_Pos := L_Start_Pos + L_Buffer;
    L_String_Len := L_String_Len + L_End_Pos;

    If L_String_Len >= L_CLOB_len Then
      Exit;
    End If;
   
  End Loop;

   Dbms_sql.Parse(L_Cursor, L_Sql_table, L_Sql_table.First, L_Sql_table.Last, False, Dbms_sql.Native);

   L_Execution_Val := Dbms_sql.Execute(L_Cursor);
   
   Commit;

   Dbms_sql.Close_cursor(L_Cursor);
End;
/
select * from test
0
 

Author Closing Comment

by:gswitz
ID: 31511741
It was necessary to concat the string into the clob in multiple statements.
lclob:='my ';
lclob:=lclob || ' sql';

Thanks,

G
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 22843278
glad we could help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question