Write a file to an Oracle-directory: Difference between revisions

From WickyWiki
m 3 revisions
 
mNo edit summary
Line 12: Line 12:


<syntaxhighlight lang=sql>
<syntaxhighlight lang=sql>
connect user1;
set serveroutput on
declare
 
   f utl_file.file_type;
DECLARE
begin
  v_exists BOOLEAN;
   f := utl_file.fopen('SYNC_DMP_DIR', 'Test.txt', 'w');
  v_length NUMBER;
   utl_file.put_line(f, 'line one: some text');
  v_blocksize NUMBER;
   utl_file.put_line(f, 'line two: more text');
  v_fileHandler UTL_FILE.FILE_TYPE;
   utl_file.fclose(f);
BEGIN
end;
   dbms_output.put_line('*** test write (replace)');
   v_fileHandler := UTL_FILE.FOPEN('DIR1', 'test.txt', 'W');
  UTL_FILE.PUT_LINE(v_fileHandler, 'test.txt');
   UTL_FILE.FCLOSE(v_fileHandler);
 
  dbms_output.put_line('*** test exist');
  UTL_FILE.FGETATTR('DIR1', 'test.txt', v_exists, v_length, v_blocksize);
  IF v_exists
  THEN
    dbms_output.put_line('found');
   else
    dbms_output.put_line('not found');
   END IF;
EXCEPTION
  WHEN OTHERS THEN
  BEGIN
    dbms_output.put_line(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE || SQLERRM);
  END;
END;
/
/
</syntaxhighlight>
</syntaxhighlight>
[[Category:SynUpd]]
[[Category:Oracle]]
[[Category:Oracle]]
[[Category:Oracle PL/SQL]]
[[Category:Oracle PL/SQL]]
[[Category:2009]]
[[Category:2009]]

Revision as of 15:37, 22 January 2014

Create Oracle directory:

connect system
create or replace directory dir1 as 'C:/TEMP';
grant read, write on directory dir1 to user1;

Note: the Oracle directory will be accessed from the Oracle server directly.

Read/write file in directory:

set serveroutput on

DECLARE
   v_exists BOOLEAN; 
   v_length NUMBER; 
   v_blocksize NUMBER; 
   v_fileHandler UTL_FILE.FILE_TYPE;
BEGIN 
  dbms_output.put_line('*** test write (replace)');
  v_fileHandler := UTL_FILE.FOPEN('DIR1', 'test.txt', 'W');
  UTL_FILE.PUT_LINE(v_fileHandler, 'test.txt');
  UTL_FILE.FCLOSE(v_fileHandler);

  dbms_output.put_line('*** test exist');
  UTL_FILE.FGETATTR('DIR1', 'test.txt', v_exists, v_length, v_blocksize); 
  IF v_exists 
  THEN 
    dbms_output.put_line('found');
  else 
    dbms_output.put_line('not found');
  END IF; 
EXCEPTION
  WHEN OTHERS THEN
  BEGIN
    dbms_output.put_line(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE || SQLERRM);
  END;
END;
/