[SOLVED] Inserting large XML document into Oracle


I have a table in Oracle:

CREATE TABLE foo (id NUMBER, document XMLType)

and I would typically insert data into it with SQL:

INSERT INTO foo VALUES (1, XMLType('<parent><child></child></parent>'))

However, I now have a document which is more than the maximum size for a string literal in PL/SQL (4000 characters). Is it possible to insert such a document in a single SQL script, without resorting to writing it to a file and then referencing that file in the sql… as that option is not open to us for reasons. I have tried:

  • inserting 4000 characters, then appending the rest in chunks – this does not work as the intermediate steps fail XML validation
  • temporarily changing the column type to CLOB etc. – Oracle forbids the conversion of columns between major types.


In SQL, concatenate 4000 byte strings to EMPTY_CLOB():

INSERT INTO foo (id, document)
  || 'first 4000 bytes...'
  || 'second 4000 bytes...'
  || 'etc.'

In PL/SQL, the limit for strings is 32,000 bytes:

  v_id  NUMBER := 1;
  v_xml VARCHAR2(32000) := 'your 32k XML string';
  INSERT INTO foo(id, document) VALUES (v_id, XMLTYPE(v_xml));

Otherwise, you can use the same technique as the SQL answer in PL/SQL:

  v_id  NUMBER := 1;
  v_xml CLOB := EMPTY_CLOB()
                || 'first 32k XML string'
                || 'second 32k XML string'
                || 'etc.';
  INSERT INTO foo(id, document) VALUES (v_id, XMLTYPE(v_xml));

Answered By – MT0

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.