Blob Datatype in Oracle
Blob Datatype in Oracle
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.
Execution Step:Login to UNIX box of DB tier of instance. One can find the hostname using the ping command in cmd.
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.