- Declare a File Variable
- Open a File using File Variable
- Write the data into the file using File Variable
- Close the file using File Variable.
select value from v$parameter where name like 'utl_file_dir'
If the above select statement doesn't return any records, that means there is no defined file directory path. This will be taken care by DBA's. Else if you want to create a directroy path follow the below steps
- Connect as SYSDBA
- CREATE OR REPLACE DIRECTORY
AS '/PATH' - GRANT READ, WRITE ON DIRECTORY
TO SCOTT; - ALTER SYSTEM SET UTL_FILE_DIR = '/PATH' SCOPE=SPFILE
- Declare a File Variable:
Ex: fv UTL_FILE.FILE_TYPE;
- Open a File using File Variable
Ex: fv := UTL_FILE.FOPEN('C:/TEMP','SAM.txt','W');
- Write the data into the File
Ex: UTL_FILE.PUT_LINE(fv,'Hello');
- Close the File
Ex: UTL_FILE.FCLOSE(fv);
Sample piece of UTL File Code,
create or replace procedure utl_sample
is
l_file_var utl_file.file_type;
begin
l_file_var := utl_file.fopen('C;/temp','SAM.txt','W');
utl_file.put_line(l_file_var,'This is a sample prog');
utl_file.fclose(l_file_var);
end;