OraLobEditor - Edit Oracle LOB (BLOB, CLOB) data »

Download OraLobEditor Free Trial »

Loading Data to Oracle LOBs Using Only SQL - (< 4K)


Overview 

The following example is part of the Oracle LOB Examples Collection. 
This example provides a demonstration of how to load data into different LOB columns using only SQL. It is possible to insert data directly into LOB columns up to 4K. Even though you are only accessing the locator, the data is stored as appropriate behind the scenes. When inserting directly into a BLOB, either the string must be a hex as an implicit HEXTORAW will be done or you can call UTL_RAW.CAST_TO_RAW('your string') to convert it for you. Note that '48656C6C6F' = 'Hello'. 

After inserting data into our test_lob table, we can select it back (with the exception of the BFILE column). When SELECTing using SQL*Plus, that we are only fetching the LOB locator. SQL*Plus will also then fetch the corresponding data. If we were to use a 3GL or PL/SQL, we can insert data from a character string (like in our example), but not select it back like we can do within SQL*Plus. 





--------------------------------------------------------------------------------

Example 

DROP TABLE test_bfile CASCADE CONSTRAINTS
/

Table dropped.


CREATE TABLE test_lob (
      id           NUMBER(15)
    , clob_field   CLOB
    , blob_field   BLOB
    , bfile_field  BFILE
)
/

Table created.


CREATE OR REPLACE DIRECTORY
    EXAMPLE_LOB_DIR
    AS
    '/u01/app/oracle/lobs'
/

Directory created.


INSERT INTO test_lob
    VALUES (  1001
            , 'Some data for record 1001'
            , '48656C6C6F' || UTL_RAW.CAST_TO_RAW(' there!') 
            , BFILENAME('EXAMPLE_LOB_DIR', 'file1.txt')
    );

1 row created.


COMMIT;

Commit complete.


SELECT
      id
    , clob_field "Clob"
    , UTL_RAW.CAST_TO_VARCHAR2(blob_field) "Blob"
FROM test_lob;

  Id Clob                      Blob
---- ------------------------- -------------
1001 Some data for record 1001 Hello there!