Convert BLOB and CLOB
Convert BLOB and CLOB
Convert BLOB and CLOB
Asfaw Gedamu
• BLOB stands for Binary Large Object. It is a data type that can store large amounts of
binary data, such as images, audio files, and videos. BLOBs are stored in a separate
location from the database tables, and they can be very large, up to 4 gigabytes in size.
• CLOB stands for Character Large Object. It is a data type that can store large amounts of
character data, such as text documents and email messages. CLOBs are stored in a
separate location from the database tables, and they can be very large, up to 4 gigabytes
in size.
Here is a visualization of how BLOBs and CLOBs are stored in Oracle Database:
Database
|
|-- Table1 (VARCHAR2 column)
|
|-- Table2 (BLOB column)
|
|-- Table3 (CLOB column)
|
`-- ...
As you can see, BLOB and CLOB columns are stored in separate tables from the other columns
in the database. This is because BLOBs and CLOBs can be very large, and storing them in
separate tables makes it easier to manage them.
Here are some examples of how BLOBs and CLOBs can be used in Oracle Database:
BLOBs and CLOBs are powerful data types that can be used to store large amounts of data in
Oracle Database. They are a valuable tool for storing binary data and character data, and they can
be used in a variety of applications.
AS
l_clob CLOB;
l_dest_offset PLS_INTEGER := 1;
l_src_offset PLS_INTEGER := 1;
l_lang_context PLS_INTEGER := DBMS_LOB.default_lang_ctx;
l_warning PLS_INTEGER;
BEGIN
DBMS_LOB.createTemporary(
lob_loc => l_clob,
cache => TRUE);
DBMS_LOB.converttoclob(
dest_lob => l_clob,
src_blob => p_data,
amount => DBMS_LOB.lobmaxsize,
dest_offset => l_dest_offset,
src_offset => l_src_offset,
blob_csid => DBMS_LOB.default_csid,
lang_context => l_lang_context,
warning => l_warning);
RETURN l_clob;
END;
/
The SQL script is a function that converts a BLOB data type to a CLOB data type. The function
takes in a BLOB data type as input and returns a CLOB data type as output. The function uses
the DBMS_LOB package to create a temporary CLOB object and then converts the BLOB data
type to the CLOB data type using the converttoclob() function.
p_blob IN BLOB,
p_dir IN VARCHAR2,
p_filename IN VARCHAR2
AS
l_file UTL_FILE.FILE_TYPE;
l_buffer RAW(32767);
l_pos INTEGER := 1;
l_blob_len INTEGER;
BEGIN
l_blob_len := DBMS_LOB.getlength(p_blob);
-- Read chunks of the BLOB and write them to the file until complete.
END LOOP;
UTL_FILE.fclose(l_file);
EXCEPTION
IF UTL_FILE.is_open(l_file) THEN
UTL_FILE.fclose(l_file);
END IF;
RAISE;
END blob_to_file;
/
The SQL script is a procedure that takes in a BLOB (Binary Large Object) as input and
writes it to a file in the specified directory. The procedure reads chunks of the BLOB and
writes them to the file until complete. It uses the UTL_FILE package to write the file. The
procedure , blob_to_file , has three parameters: p_blob (the BLOB to write), p_dir (the
directory where the file should be written), and p_filename (the name of the file to write).
This script uses the DBMS_LOB.createtemporary procedure to create a temporary BLOB object.
The procedure does not need to allocate a new BLOB object each time the function is called. The
script also uses the DBMS_LOB.lobmaxsize constant to specify the maximum amount of data to
convert. This can help to prevent the function from converting too much data, which could lead
to an error.