05.11.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 15 ■ DATA LOADING AND UNLOADING 699<br />

181 execute immediate<br />

182 'alter session set nls_date_format=''' || l_datefmt || '''';<br />

183 return l_cnt;<br />

184 exception<br />

185 /*<br />

186 In the event of ANY error, reset the data format <strong>and</strong><br />

187 re-raise the error.<br />

188 */<br />

189 when others then<br />

190 execute immediate<br />

191 'alter session set nls_date_format=''' || l_datefmt || '''';<br />

192 RAISE;<br />

193 end;<br />

194 end run;<br />

195<br />

196<br />

197 end unloader;<br />

198 /<br />

Package body created.<br />

To run this, we can simply use the following (note that the following does, of course,<br />

require that you have SELECT on SCOTT.EMP granted to one of your roles or to yourself directly):<br />

ops$tkyte@ORA10G> set serveroutput on<br />

ops$tkyte@ORA10G> create or replace directory my_dir as '/tmp';<br />

Directory created.<br />

ops$tkyte@ORA10G> declare<br />

2 l_rows number;<br />

3 begin<br />

4 l_rows := unloader.run<br />

5 ( p_query => 'select * from scott.emp order by empno',<br />

6 p_tname => 'emp',<br />

7 p_mode => 'replace',<br />

8 p_dir => 'MY_DIR',<br />

9 p_filename => 'emp',<br />

10 p_separator => ',',<br />

11 p_enclosure => '"',<br />

12 p_terminator => '~' );<br />

13<br />

14 dbms_output.put_line( to_char(l_rows) ||<br />

15 ' rows extracted to ascii file' );<br />

16 end;<br />

17 /<br />

14 rows extracted to ascii file<br />

PL/SQL procedure successfully completed.

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

Saved successfully!

Ooh no, something went wrong!