Tuesday, 11 June 2013

Displaying Large CLOBS in Oracle APEX

I had a problem retrieving a clob from the database and displaying it in an apex text area.  The reason for this is it is a very large clob and Apex areas have a 32k character (byte) limit. This is because PL/SQL treats oracle apex page items as varchars not clobs and varchars have a maximum size. Anything over that size will not be displayed properly.

So a PL/SQL process such as

select clob_field
into :P20_CLOB_ITEM
from table
where id = :P20_CLOB_ID

won't work for very large clobs. It won't throw an error it just won't display

To get round this problem you have to use apex.ajax.clob. This uses collections to process large clobs and write them to the text area. Firstly you need to select the CLOB into the collection using the following procedure. Run this on the same page that has the id value for the row that you are getting the clob from

l_clob clob:= empty_clob();


if apex_collection.collection_exists(p_collection_name=>'CLOB_CONTENT') then
end if;

dbms_lob.createtemporary( l_clob, false, dbms_lob.SESSION );

select clob_field
into l_clob
from table
where id = :P19_CLOB_ID;

apex_collection.add_member(p_collection_name => 'CLOB_CONTENT',p_clob001 => l_clob);

Then on the page with the text area put the following javascript code in the HTML Header of the page

<script type="text/javascript">
function clob_get(){
        var clob_ob = new apex.ajax.clob(
                var rs = p.readyState
                if(rs == 1||rs == 2||rs == 3){
                }else if(rs == 4){
                }else{return false;}

p.responseText retrieves the clob value from the collection CLOB_CONTENT

then call the javascript. in my case I place this in the html body of the page

onload = "javascript:clob_get();"

Hope this helps as it caused me quite a few problems.

for more on how to use this check out a blog post that was very helpful to me in solving this problem


  1. Thank you so much this was very helpful!!

  2. Some things to note (that may or may not be obvious):
    - The SQL needs to be changed to reference the appropriate CLOB field and table as required.
    - Source type that should be used on the Apex Page Item is PL/SQL Function body
    - In the java script header code - P20_CLOB_ITEM should be replaced by the page item name of the text area field to be populated

  3. Thank you for your comment. Yes I used generic item and variable names that you will have to change for your specific circumstances. From now on I will make that more explicit.

  4. Works Like charm - Thank you Mayo and Yanizm for the GR8 comment

  5. Hi Mayo, This does work displaying 1 clob item on a page but what if multiple clob items need to be displayed on a page? (ie..P20_CLOB_ITEM and P20_CLOB_ITEM2) I am not seeing the obvious solution if it is present.

    Thank you.

    1. Hi Matt. I'm not sure how to solve that as I have never had that requirement. If I find a solution I will post it. If you find a solution please link to it in the comments as I'm sure someone else will have the same problem

  6. You legend Mayo, your blog helped me again! Hope you are keeping well, pop by sometime!