Beruflich Dokumente
Kultur Dokumente
package.
UTL_FILE is an oracle pl/sql package that is supplied to allow PL/SQL to read and create text files
in the file system.
UTL_FILE can only read and create text files. Specifically, it cannot be used to read or create
binary files.
UTL_FILE is an appropriate tool for creating reports in the form of flat file from the database .
UTL_FILE is also used for reading files.
The following steps must be followed in order to run UTL_FILE package.
Step 1) Connect as sys database user
Step 2) Create a directory:
SQL> CREATE OR REPLACE DIRECTORY utl_file_dir AS E:\PLSQL;
Directory created.
In the above query utl_file_dir is the logical name for the path E:\PLSQL.
We can mention the logical name utl_file_dir inside the program in uppercase within single quotes
(utl_file_dir is mapped to the directory E:\PLSQL)
We can create any number of logical path names(DBA directories) in oracle 10g.
Step 3) Grant read and write on the directory
SQL> grant read,write on directory utl_file_dir to scott;
Grant succeeded.
Step 4) connect as scott database user
SQL>CONNECT SCOTT/*******
Example
The following is the procedure to write a database table contents to a text file.
This PL/SQL Procedure write the contents of the database table emp in the scott schema to a text
file called emp_table.txt in the windows directory E:\PLSQL
SQL> conn scott/tiger
Connected.
SQL> show user
USER is SCOTT
ENAME
COMM
JOB
MGR
HIREDATE
DEPTNO;
UTL_FILE.PUTF(file1, head);
utl_file.new_line(file1);
line:=
=================================================================================
=================;
UTL_FILE.PUTF(file1, line);
utl_file.new_line(file1);
for employ in empc loop
stmt := rpad(employ.empno,10, ) ||
rpad(employ.ename,20, ) ||
rpad(employ.job,20, ) ||
rpad(nvl(to_char(employ.mgr), ),30, ) ||
rpad(employ.hiredate,30, ) ||
rpad(employ.sal,30, ) ||
rpad(nvl(to_char(employ.comm), ),25, ) ||
rpad(employ.deptno,8, );
utl_file.PUTF(file1, stmt);
utl_file.new_line(file1);
end loop;
utl_file.fclose(file1);
end;
/
Procedure created.
SQL> execute write_file;
PL/SQL procedure successfully completed.
Step 2) After execution of the above procedure a new file emp_table.txt is created in the location
E:\PLSQL as shown in the below screenshot.
NUMBER := 0;
V_CR_AMT
NUMBER := 0;
V_SUCCESS_COUNTER NUMBER := 0;
V_LOOSER_COUNTER NUMBER := 0;
V_TOTAL_COUNTER NUMBER := 0;
V_NEGATIVE_COUNTER NUMBER := 0;
BEGIN
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT,'POST PAYROLL HEALTH
CHECK');
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT,'================
=============');
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT, ' ');
BEGIN
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT,'DEBIT CREDIT
DIFFERENCES');
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT,'================
========');
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT, ' ');
FOR X IN (
SELECT PAAF.ASSIGNMENT_NUMBER EMP_NO,
PETF.ELEMENT_NAME,
PPA.DATE_EARNED,
TO_NUMBER(PRRV.RESULT_VALUE) AMT
PETF,
PRR,
PAY_RUN_RESULT_VALUES PRRV,
PAY_ALL_PAYROLLS_F
PYLPF,
PAY_INPUT_VALUES_F
PIVF,
PAY_PAYROLL_ACTIONS
PPA,
PAY_ASSIGNMENT_ACTIONS PAA,
GLOBAL_VARIABLES
GV
END LOOP;
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT, ' ');
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT,'TOTAL RECORD
PROCESSED : ' || V_TOTAL_COUNTER);
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT,'TOTAL RECORD
SUCCESSFULY TESTED : ' ||V_SUCCESS_COUNTER);
APPS.FND_FILE.PUT_LINE(APPS.FND_FILE.OUTPUT,'TOTAL RECORD REJECTED
: ' || V_LOOSER_COUNTER);
END;
END XXXXXX;