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 695<br />

■Note The IS_WINDOWS function does rely on you using \ in your CONTROL_FILES parameter. Be aware<br />

that you may use /, but it would be highly unusual.<br />

The following is a procedure to create a control file to reload the unloaded data, using the<br />

DESCRIBE table generated by DBMS_SQL.DESCRIBE_COLUMNS. It takes care of the operating system<br />

specifics for us, such as whether the operating system uses a carriage return/linefeed (this is<br />

used for the STR attribute):<br />

28<br />

29 procedure dump_ctl( p_dir in varchar2,<br />

30 p_filename in varchar2,<br />

31 p_tname in varchar2,<br />

32 p_mode in varchar2,<br />

33 p_separator in varchar2,<br />

34 p_enclosure in varchar2,<br />

35 p_terminator in varchar2 )<br />

36 is<br />

37 l_output utl_file.file_type;<br />

38 l_sep varchar2(5);<br />

39 l_str varchar2(5) := chr(10);<br />

40<br />

41 begin<br />

42 if ( is_windows )<br />

43 then<br />

44 l_str := chr(13) || chr(10);<br />

45 end if;<br />

46<br />

47 l_output := utl_file.fopen( p_dir, p_filename || '.ctl', 'w' );<br />

48<br />

49 utl_file.put_line( l_output, 'load data' );<br />

50 utl_file.put_line( l_output, 'infile ''' ||<br />

51 p_filename || '.dat'' "str x''' ||<br />

52 utl_raw.cast_to_raw( p_terminator ||<br />

53 l_str ) || '''"' );<br />

54 utl_file.put_line( l_output, 'into table ' || p_tname );<br />

55 utl_file.put_line( l_output, p_mode );<br />

56 utl_file.put_line( l_output, 'fields terminated by X''' ||<br />

57 to_hex(p_separator) ||<br />

58 ''' enclosed by X''' ||<br />

59 to_hex(p_enclosure) || ''' ' );<br />

60 utl_file.put_line( l_output, '(' );<br />

61<br />

62 for i in 1 .. g_descTbl.count<br />

63 loop<br />

64 if ( g_descTbl(i).col_type = 12 )

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

Saved successfully!

Ooh no, something went wrong!