Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to files using utl_file in oracle

Tags:

plsql

How to use put function.my procedure is not compiling with put. but putline is working fine. i want to print in the same line

like image 318
rohit kumar Avatar asked Sep 20 '10 09:09

rohit kumar


People also ask

What is the use of UTL_FILE in Oracle?

The UTL_FILE package lets your PL/SQL programs read and write operating system (OS) text files. It provides a restricted version of standard OS stream file input/output (I/O).

How do you write data into a file in PL SQL?

SQL> -- Simple PLSQL to open a file, SQL> -- write two lines into the file, SQL> -- and close the file SQL> declare 2 fhandle utl_file. file_type; 3 begin 4 fhandle := utl_file. fopen( 5 'UTL_DIR' -- File location 6 , 'test_file.

Which method in UTL_FILE is used to open the file?

FOPEN returns a file handle, which must be passed to all subsequent procedures that operate on that file. The specific contents of the file handle are private to the UTL_FILE package, and individual components should not be referenced or changed by the UTL_FILE user. Handle to open file.

What does UTL_FILE Fflush do?

This procedure flushes the pending data identified by the file handle into the file. This procedure is useful when we want to read the file while it is still open. For example, the debugging and logging files can be read immediately once they are flushed with messages.


1 Answers

Here's an example of code which uses the UTL_FILE.PUT and UTL_FILE.PUT_LINE calls:

declare 
  fHandle  UTL_FILE.FILE_TYPE;
begin
  fHandle := UTL_FILE.FOPEN('my_directory', 'test_file', 'w');

  UTL_FILE.PUT(fHandle, 'This is the first line');
  UTL_FILE.PUT(fHandle, 'This is the second line');
  UTL_FILE.PUT_LINE(fHandle, 'This is the third line');

  UTL_FILE.FCLOSE(fHandle);
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Exception: SQLCODE=' || SQLCODE || '  SQLERRM=' || SQLERRM);
    RAISE;
end;

The output from this looks like:

This is the first lineThis is the second lineThis is the third line

Share and enjoy.

like image 76