First you need to understand what LOB is. This is "big data", perhaps more than any other data type in Oracle. They are similar to regular files in the file system. To write a file to a file you need
- open the file for writing
- crop file if you want to start filling it from scratch
- read source data in chunks in a loop
- add your pieces of data to the file in the same loop, one by one
- close file
More or less the same is true for LOB. In your table, the LOB column (CLOB / BLOB / NCLOB) is just a pointer / link to another place on your disk where the actual data is stored. In standard Oracle expressions, a pointer is called a Large Object Locator. You need
- open / initialize large object locator
- trim the contents of the LOB if you want to start filling it from scratch
- add your chunks of data to the contents of the LOB in a loop, one by one
- close the large object locator
In PL / SQL, it might look like this:
-- create table blob_test(id number, b blob); declare v_b blob; aaa raw(32767); longLine varchar2(32767); begin longLine := LPAD('aaaa', 32767,'x'); aaa := UTL_RAW.CAST_TO_RAW(longLine); insert into blob_test values(1,empty_blob()) returning b into v_b; dbms_lob.open(v_b,dbms_lob.lob_readwrite); dbms_lob.writeappend(v_b,UTL_RAW.LENGTH (aaa) ,aaa); dbms_lob.close(LOB_LOC=>v_b); commit; end;
Explanation:
- initialize the LOB locator =
insert into blob_test values(1,empty_blob()) returning b into v_b; - open the LOB locator to write =
dbms_lob.open(v_b,dbms_lob.lob_readwrite); - trim the contents of the LOB if you want to start filling it from scratch ... This is done by calling
empty_blob() on insert . - add your pieces of data to the contents of the LOB in the loop, one after the other = there will be only one iteration of
dbms_lob.writeappend() , adding only one aaa fragment of length utl_raw.length(aaa) (maximum 32767) in LOB v_b - close the locator LOB =
dbms_lob.close(LOB_LOC=>v_b);
Arkadiusz ลukasiewicz
source share