Oracle PL/SQL Programming The PL/SQL Channel Input/Output and Oracle PL/SQL Manipulating BFILEs with DBMS_LOB Steven Feuerstein steven@stevenfeuerstein.com www.StevenFeuerstein.com www.PLSQLChallenge.com Oracle PL/SQL Programming Quick Reminders • Download code and PowerPoint documents from www.ToadWorld.com/SF (aka, "PL/SQL Obsession"). • Make sure you are comfortable with the material covered in the previous lessons in this series. – How to use UTL_FILE to read and write operating system files. Copyright 2010 Feuerstein and Associates Page 2 Oracle PL/SQL Programming DBMS_LOB and BFILEs • Oracle allows you to also manipulate binary files or BFILEs through the DBMS_LOB package. – The intention is to work with really big files, but they don't have to be. • A BFILE is a pointer to an OS file. • This will not constitute a training on working with LOBs! – It is a big and complex topic; I will simply introduce you to some of the features related to manipulating BFILEs from within a PL/SQL block. Copyright 2010 Feuerstein and Associates Page 3 Oracle PL/SQL Programming DBMS_LOB BFILE functionality • Open and close BFILEs • Compare two BFILES with COMPARE. • Get information about a BFILE – Name and location? exists? length? • Read contents of BFILE – They are read-only structures! • And more... – Read BFILE into LOB – Perform INSTR and SUBSTR operations on a BFILE. Copyright 2010 Feuerstein and Associates Page 4 Oracle PL/SQL Programming Open and Close BFILES DECLARE l_bfile BFILE := BFILENAME ('DEMO', 'exec_ddl_from_file2.sql'); BEGIN DBMS_OUTPUT.put_line ('Exists? ' || DBMS_LOB.fileexists (l_bfile)); DBMS_OUTPUT.put_line ('Open before open? ' || DBMS_LOB.fileisopen (l_bfile)); DBMS_LOB.fileopen (l_bfile); DBMS_OUTPUT.put_line ('Open after open? ' || DBMS_LOB.fileisopen (l_bfile)); DBMS_LOB.fileclose (l_bfile); DBMS_OUTPUT. put_line ('Open after close? ' || DBMS_LOB.fileisopen (l_bfile)); END; • Initialize BFILE pointer with the BFILENAME function. • Then you can open the bfile, perform operations on it (details to come), and close that file when you are done. Copyright 2010 Feuerstein and Associates bfile_open_close.sql Page 5 Oracle PL/SQL Programming Compare two BFILES with COMPARE DECLARE l_bfile1 BFILE := BFILENAME ('TEMP', 'file1.txt'); l_bfile2 BFILE := BFILENAME ('TEMP', 'file2.txt'); BEGIN DBMS_LOB.fileopen (l_bfile1); DBMS_LOB.fileopen (l_bfile2); DBMS_OUTPUT.put_line ( 'Equal? ' || DBMS_LOB.compare ( file_1 => l_bfile1, file_2 => l_bfile2 , amount => 33, offset_1 => 1, offset_2 => 1)); DBMS_LOB.fileclose (l_bfile1); DBMS_LOB.fileclose (l_bfile2); END; • Use the COMPARE program to find out if two BFILEs are the same: 0 = same, 1 = different. – Specify amount of file you want to compare, and the offset locations in each. Copyright 2010 Feuerstein and Associates bfile_compare.sql Page 6 Oracle PL/SQL Programming Get information about a BFILE • • • • Get the name of the file: FILEGETNAME Get length of the file. GETLENGTH Does the file exist? FILEEXISTS Is the file open? FILEISOPEN Copyright 2010 Feuerstein and Associates bfile_demo.sql Page 7 Oracle PL/SQL Programming Read contents of BFILE DECLARE l_bfile BFILE := BFILENAME ('DEMO', 'exec_ddl_from_file.sql'); l_contents RAW (32767); l_amount PLS_INTEGER := 100; BEGIN DBMS_LOB.fileopen (l_bfile); DBMS_LOB.read (l_bfile, l_amount, 1, l_contents); dbms_output.put_line (l_contents); DBMS_LOB.fileclose (l_bfile); END; • Use DBMS_LOB.READ to read into a RAW or VARCHAR2 variable. – But for BFILEs, only RAW. • You can pass it a BLOB or BFILE pointer. • The amount is IN OUT – it returns the actual number of bytes read. Copyright 2010 Feuerstein and Associates bfile_read.sql Page 8 Oracle PL/SQL Programming Other BFILE operations • Read a BFILE into a BLOB or CLOB. • Perform INSTR operations on the contents of a BFILE. • Perform SUBSTR operations on the contents of a BFILE. Copyright 2010 Feuerstein and Associates bfile_demo2.sql Page 9 Oracle PL/SQL Programming Conclusions • Text files can generally be managed with UTL_FILE. • When you are working with large, binary files, however, DBMS_LOB and BFILEs offer a much better solution. – Wide range of functionality in the API. • This lesson serves as an introduction to BFILEs and DBMS_LOB. – You will definitely need to study the package more closely before you start manipulating binary files with this package. Copyright 2010 Feuerstein and Associates Page 10 Oracle PL/SQL Programming Next Steps • Download the demo.zip if you have not already (www.ToadWorld.com/SF). – Run the sample code yourself to better understand the features and techniques. • Watch the other lessons in this series. – Working with UTL_FILE – System output with DBMS_OUTPUT – Email with UTL_MAIL Copyright 2010 Feuerstein and Associates Page 11