0% found this document useful (0 votes)
27 views4 pages

Blob Datatype in Oracle

This document provides steps to insert a binary file into a BLOB column in an Oracle database table. 1. Create a table with a BLOB column. 2. Create a directory object pointing to the filesystem location where files will be stored. 3. Use a PL/SQL block to insert an empty row, retrieve the BLOB locator, open the binary file, load the file contents into the BLOB, close the file, and commit.

Uploaded by

Prasu Anu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Blob Datatype in Oracle

This document provides steps to insert a binary file into a BLOB column in an Oracle database table. 1. Create a table with a BLOB column. 2. Create a directory object pointing to the filesystem location where files will be stored. 3. Use a PL/SQL block to insert an empty row, retrieve the BLOB locator, open the binary file, load the file contents into the BLOB, close the file, and commit.

Uploaded by

Prasu Anu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Q. How to insert Binary file in a table.

Step 1: Create a table in database having at least one column of datatype BLOB; Sample code for reference purpose:CREATE TABLE sam_emp ( empno number, ename VARCHAR2(50), resume BLOB DEFAULT Empty_Blob() );

STEP 2: Create a directory using the script Create or replace directory DIR_TEMP as /usr/tmp; In order to find at which locations we can place the file to moved into a table column one should check following commands:SELECT * FROM v$parameter WHERE NAME = 'utl_file_dir' It give the location where a file can be placed. Then create a directory pointing towill that location.

STEP 3: Sample code to insert the data in a table. declare f_lob bfile; b_lob blob; v_dir VARCHAR2(200):='DIR_TEMP'; begin insert into sam_emp(empno,ename,resume) values ( 9667, 'Saurav',Empty_Blob() ) return resume into b_lob; f_lob := bfilename(v_dir , 'test.rtf' ); dbms_lob.fileopen(f_lob, dbms_lob.file_readonly); dbms_lob.loadfromfile

( b_lob, f_lob, dbms_lob.getlength(f_lob) ); dbms_lob.fileclose(f_lob); commit; end; Description of above code:1> Insert statement will insert the row in the table. insert into sam_emp(empno,ename,resume) values ( 9667, 'Saurav',Empty_Blob() ) return resume into b_lob; Over here empty_blob() is being inserted in column of datatype blob.
EMPTY_BLOB returns an empty LOB locator that can be used to initialize a LOB variable or, in an INSERT or UPDATE statement, to initialize a LOB column or attribute to EMPTY.

i.e Insert a row with an empty BLOB in your table and return the locater.

2> f_lob := bfilename(v_dir , 'test.rtf' ); Point to the Word file to be loaded from the directory created in Step 1 using bfile data type. 3> dbms_lob.fileopen(f_lob, dbms_lob.file_readonly); Open the file and use the locater from Step 2 to insert the file. 4> dbms_lob.loadfromfile ( b_lob, f_lob, dbms_lob.getlength(f_lob) ); load the data from source file to table using the locator as destination reference retrieved using return clause of insert.

Close the file.

Execution Step:Login to UNIX box of DB tier of instance. One can find the hostname using the ping command in cmd.

Open the directory /usr/tmp

And transfer the file in transfer mode binary. Set the previleges to 0777 for the file. Execute the anonymous pl/sql procedure. One can verify the same by retrieving the data from the table.

You might also like