Thursday, August 19, 2010

Oracle Tips: Writing to File

Need to have these executed before you can write to files on server

1. CREATE DIRECTORY utl_dir1 AS '<path>';
2. GRANT ALL ON DIRECTORY utl_dir1 TO <owner>;

The following code demonstrates how to write the contents of a table to a file.
Declare
f utl_file.file_type
indx number:=1;
f  := utl_file.fopen('UTL_DIR1',<file_name>,'W');

CURSOR <cursor_name>
  IS
    SELECT *
    FROM <table_name>;

BEGIN
indx:=1;
FOR <row_variable> IN <cursor_name>
LOOP
  utl_file.put_line(f,convert_row_to_delim_str(fxd_loan_row,','));
  indx:=indx+1;
END LOOP;

utl_file.fclose(f);

END
/

No comments: