Convert BLOB and CLOB

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Convert File, BLOB and CLOB Datatypes

Using PL/SQL Scripts

Asfaw Gedamu

Download documents from:


https://t.me/paragonacademy
July 30,2023
BLOB and CLOB are data types in Oracle Database that are used to store large amounts of
binary data and character data, respectively.

• 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:

• A BLOB column could be used to store an image of a product in an e-commerce


database.
• A CLOB column could be used to store the text of a blog post in a blog database.
• A BLOB column could be used to store a video of a product demonstration in a training
database.
• A CLOB column could be used to store the text of a user manual in a product support
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.

1. Convert a BLOB datatype to a CLOB datatype

CREATE OR REPLACE FUNCTION blob_to_clob (p_data IN BLOB)


RETURN CLOB

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.

2. Convert the contents of a BLOB datatype to a file

CREATE OR REPLACE PROCEDURE blob_to_file (

p_blob IN BLOB,

p_dir IN VARCHAR2,

p_filename IN VARCHAR2

AS

l_file UTL_FILE.FILE_TYPE;

l_buffer RAW(32767);

l_amount BINARY_INTEGER := 32767;

l_pos INTEGER := 1;
l_blob_len INTEGER;

BEGIN

l_blob_len := DBMS_LOB.getlength(p_blob);

-- Open the destination file.

l_file := UTL_FILE.fopen(p_dir, p_filename,'wb', 32767);

-- Read chunks of the BLOB and write them to the file until complete.

WHILE l_pos <= l_blob_len LOOP

DBMS_LOB.read(p_blob, l_amount, l_pos, l_buffer);

UTL_FILE.put_raw(l_file, l_buffer, TRUE);

l_pos := l_pos + l_amount;

END LOOP;

-- Close the file.

UTL_FILE.fclose(l_file);

EXCEPTION

WHEN OTHERS THEN

-- Close the file if something goes wrong.

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).

3. Convert a CLOB datatype to a BLOB datatype

CREATE OR REPLACE FUNCTION clob_to_blob (p_clob IN CLOB)


RETURN BLOB
AS
BEGIN
-- Create a temporary BLOB object.
l_blob := DBMS_LOB.createtemporary(cache => TRUE);

-- Convert the CLOB to a BLOB.


DBMS_LOB.converttoblob(
dest_lob => l_blob,
src_clob => p_clob,
amount => DBMS_LOB.lobmaxsize);

-- Return the BLOB object.


RETURN l_blob;
END;
/

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.

4. Convert the contents of a CLOB datatype to a file

CREATE OR REPLACE PROCEDURE clob_to_file(p_clob IN CLOB,


p_dir IN VARCHAR2,
p_filename IN VARCHAR2)
AS
l_file UTL_FILE.FILE_TYPE;
l_buffer VARCHAR2(32767);
l_amount BINARY_INTEGER := 32767;
l_pos INTEGER := 1;
BEGIN
l_file := UTL_FILE.fopen(p_dir, p_filename, 'w', 32767);
LOOP
DBMS_LOB.read(p_clob, l_amount, l_pos, l_buffer);
UTL_FILE.put(l_file, l_buffer);
l_pos := l_pos + l_amount;
EXIT WHEN l_amount > DBMS_LOB.getlength(p_clob) - l_pos + 1;
END LOOP;
UTL_FILE.fclose(l_file);
EXCEPTION
WHEN OTHERS THEN
IF UTL_FILE.is_open(l_file) THEN
UTL_FILE.fclose(l_file);
END IF;
RAISE;
END clob_to_file;
/
The script takes in three parameters: a CLOB (Character Large Object), a directory path and a
filename. It reads the CLOB data and writes it to a file in the specified directory with the
specified filename. The file is created or replaced if it already exists. The procedure uses the
UTL_FILE package to write the data to the file. This package provides a restricted version of
operating system file I/O suitable for writing files from SQL and PL/SQL programs.

5. Convert the contents of a file to a BLOB datatype

CREATE OR REPLACE PROCEDURE convert_file_to_blob (


p_file_name IN VARCHAR2,
p_blob OUT BLOB)
AS
l_file UTL_FILE.FILE_TYPE;
BEGIN
l_file := UTL_FILE.fopen(p_file_name, 'r');

-- Create a temporary BLOB object.


p_blob := DBMS_LOB.createtemporary(cache => TRUE);

-- Read the file into the BLOB.


DBMS_LOB.loadfromfile(
dest_lob => p_blob,
src_file => l_file);

-- Close the file.


UTL_FILE.fclose(l_file);
END;
/

6. Convert the contents of a file to a CLOB datatype


CREATE OR REPLACE PROCEDURE convert_file_to_clob (
p_file_name IN VARCHAR2,
p_clob OUT CLOB)
AS
l_file UTL_FILE.FILE_TYPE;
BEGIN
l_file := UTL_FILE.fopen(p_file_name, 'r');

-- Create a temporary CLOB object.


p_clob := DBMS_LOB.createtemporary(cache => TRUE);

-- Read the file into the CLOB.


l_buffer := UTL_FILE.get_line(l_file, 32767);
WHILE l_buffer IS NOT NULL LOOP
DBMS_LOB.append(p_clob, l_buffer);
l_buffer := UTL_FILE.get_line(l_file, 32767);
END LOOP;

-- Close the file.


UTL_FILE.fclose(l_file);
END;
/

You might also like