1

I am trying to update a clob value with length > 2 million characters in PL/SQL. I am getting the error

String literal too long

Is there any way I can get around this error?

This is the PL/SQL code snippet I am trying to update the clob value with:

 DECLARE 
   value clob; 
   clob_field clob;
   fromindex integer;
   offset integer;
   chunks integer;
   eclob clob;
   sql_stmt clob;
BEGIN 
   fromindex := 1;
   offset := 2;  
   clob_field := '<clob_value_with_length_2Million>';
   chunks := 1+(dbms_lob.Getlength(clob_field) / 2);
   value :='';
   
   FOR chunk IN 1 .. chunks LOOP 
     IF ( chunk != 1) THEN
       value := value || ' || ';
     END IF;
     value := value || 'to_clob('''||dbms_lob.Substr(clob_field, offset, fromindex)||''')';
     fromindex := fromindex + 2;
   END LOOP; 
   dbms_output.put_line(value); 
  sql_stmt := 'update mytable
  set sources = ' || value ||' where scenario_id = 1 and entry_index = 1';
  EXECUTE IMMEDIATE sql_stmt;
END; 

I am getting the error at clob_field initialization and it is obvious as PL/SQl wont allow more than 32k characters. So, I am reaching out here to see if I can have any solution to my problem.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mallikarjuna
  • 21
  • 1
  • 4

1 Answers1

0

You can reduce the amount of code needed and improve performance of your code if you used bind variables. If you are attempting to build a different update statement each time, the database will need to come up with a execution plan for each different query. Using bind variables also removes the need of having to do any SQL sanitization to protect against SQL injection.

Example

SQL> CREATE TABLE mytable
  2  AS
  3      SELECT 1 AS scenario_id, 1 AS entry_index, EMPTY_CLOB () || 'clob1' AS sources FROM DUAL
  4      UNION ALL
  5      SELECT 2 AS scenario_id, 2 AS entry_index, EMPTY_CLOB () || 'clob2' AS sources FROM DUAL;

Table created.

SQL> SELECT * FROM mytable;

SCENARIO_ID ENTRY_INDEX SOURCES
----------- ----------- --------------------------------------------------------------------------------
          1           1 clob1
          2           2 clob2

SQL> DECLARE
  2      clob_field      CLOB;
  3      l_scenario_id   NUMBER;
  4      l_entry_index   NUMBER;
  5      sql_stmt        CLOB;
  6  BEGIN
  7      clob_field := '<clob_value_with_length_2Million>';
  8      l_scenario_id := 1;
  9      l_entry_index := 1;
 10
 11      sql_stmt :=
 12          'update mytable set sources = :bind_clob where scenario_id = :scenario and entry_index = :entry';
 13
 14      EXECUTE IMMEDIATE sql_stmt
 15          USING clob_field, l_scenario_id, l_entry_index;
 16  END;
 17  /

PL/SQL procedure successfully completed.

SQL> SELECT * FROM mytable;

SCENARIO_ID ENTRY_INDEX SOURCES
----------- ----------- --------------------------------------------------------------------------------
          1           1 <clob_value_with_length_2Million>
          2           2 clob2
EJ Egyed
  • 5,791
  • 1
  • 8
  • 23