0

i have a text XML with more than 100K Characters , I am trying to write a PLSQL Block to insert this XML into an XMLTYPE Oracle Table. Below is my PLSQL Block and it wont allow to insert the xml and getting error saying string literal is too long because Oracle's SQL can only handle up to 4000 characters . I cannot change the datatype in Oracle. How i can insert this XML ?

Table Column is REQUEST_XML    XMLTYPE

I was trying like this but it seems that also not working.

INSERT INTO XYZ.ABC(ID,REQUEST_XML) values(123,yourXmlStr);

PLSQL is below,

DECLARE
yourXmlStr   xmltype := xmltype('<DRIVEResponse TimeZone="EDT">
<Condition1  ActionStep="ABCABC"  /> // This can be more than 100K Characters
</DRIVEResponse>');
BEGIN
  INSERT INTO XYZ.ABC(ID,REQUEST_XML) values(123,XMLTYPE.CREATEXML(yourXmlStr));
COMMIT;
END;
2
  • What is your table structure? Are you sure the CreateXML is not truncating it instead of the table itself? Commented Mar 17, 2021 at 18:17
  • Update the question . And no CreateXML not truncating it, i still see 100K Characters Commented Mar 17, 2021 at 18:22

1 Answer 1

2

You're creating an xmltype twice, which is unnecessary. Your source data is a string literal (anything between two single quotes), which in PL/SQL has a maximum length of 4000 characters (or 32767, see MAX_STRING_SIZE). Fortunately, the xmltype constructor can also operate on a clob. You don't say where the source data is coming from, so I'm not sure of the best way to construct that:

declare
    l_clob    clob;
    l_xml     xmltype;
begin
    l_clob := '<DRIVEResponse TimeZone="EDT">';
    l_clob := l_clob || '<Condition1  ActionStep="ABCABC"  />';
    ... repeat in chunks of less than 32768 characters ...
    l_clob := l_clob || '</DRIVEResponse>';
    l_xml := xmltype.createxml( l_clob );
    
    INSERT INTO XYZ.ABC(ID,REQUEST_XML) values(123, l_xml);
end;
Sign up to request clarification or add additional context in comments.

4 Comments

So the Chunk of 4000 characters has to created in my service layer before calling Oracle ?
That's going to depend. You could create a clob and use it in your insert statement with ... values(123, XMLTYPE.CREATEXML(:mylob)). I'd argue this would be a good candidate for a stored procedure taking a clob as a parameter. I'm not sure about using clobs as bind variables; I get the impression that can be a bit finicky.
@eaolson The maximum VARCHAR2 variable size in PL/SQL is 32767 bytes, per the PL/SQL Program Limits. That value is necessarily the same as the maximum size of a VARCHAR2 in SQL - which you are right is either 4000 or 32767.
To be really pedantic, that's a string literal, not a varchar2. They're slightly different, though I admit the difference here is largely academic. String literals use blank-padded comparison, like chars, for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.