05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

672<br />

CHAPTER 15 ■ DATA LOADING AND UNLOADING<br />

• On lines 11 <strong>and</strong> 12, we load the entire contents of the operating system file /tmp/<br />

test.txt into the LOB locator we just inserted. We use DBMS_LOB.GETLENGTH() to tell<br />

the LOADFROMFILE() routine how many bytes of the BFILE to load (all of them).<br />

• Lastly, on line 14, we close the BFILE we opened, <strong>and</strong> the CLOB is loaded.<br />

If we had attempted to use dir1 instead of DIR1 in the preceding example, we would have<br />

encountered the following error:<br />

ops$tkyte@ORA10G> declare<br />

...<br />

6 returning theclob into l_clob;<br />

7<br />

8 l_bfile := bfilename( 'dir1', 'test.txt' );<br />

9 dbms_lob.fileopen( l_bfile );<br />

...<br />

15 end;<br />

16 /<br />

declare<br />

*<br />

ERROR at line 1:<br />

ORA-22285: non-existent directory or file for FILEOPEN operation<br />

ORA-06512: at "SYS.DBMS_LOB", line 523<br />

ORA-06512: at line 9<br />

This is because the directory dir1 does not exist—DIR1 does. If you prefer to use directory<br />

names in mixed case, you should use quoted identifiers when creating them as we did for<br />

dir2. This will allow you to write code as shown here:<br />

ops$tkyte@ORA10G> declare<br />

2 l_clob clob;<br />

3 l_bfile bfile;<br />

4 begin<br />

5 insert into demo values ( 1, empty_clob() )<br />

6 returning theclob into l_clob;<br />

7<br />

8 l_bfile := bfilename( 'dir2', 'test.txt' );<br />

9 dbms_lob.fileopen( l_bfile );<br />

10<br />

11 dbms_lob.loadfromfile( l_clob, l_bfile,<br />

12 dbms_lob.getlength( l_bfile ) );<br />

13<br />

14 dbms_lob.fileclose( l_bfile );<br />

15 end;<br />

16 /<br />

PL/SQL procedure successfully completed.<br />

There are methods other than the load from file routines by which you can populate a<br />

LOB using PL/SQL. Using DBMS_LOB <strong>and</strong> its supplied routines is by far the easiest if you are<br />

going to load the entire file. If you need to process the contents of the file while loading it,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!