flat file access
TRANSCRIPT
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 1/18
Accessing Flat Files
• Three Different Ways
UTL_FILE
Oracle Loader
SQL Loader
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 2/18
What Is the UTL_FILE Package?
• Extends I/O to text files within PL/SQL
• Provides security for directories on the serverthrough the init.ora file
• Is similar to standard operating system I/O – Open files
– Get text
–
Put text – Close files
– Use the exceptions specific to the UTL_FILE
package
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 3/18
YesGet lines
from the
text file
Put lines
into the
text file
Morelines to
process?
No Close
the
text file
File Processing Using the
UTL_FILE Package
Open the
text file
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 4/18
UTL_FILE Procedures and
Functions• Function FOPEN
• Function IS_OPEN
• Procedure GET_LINE• Procedure PUT, PUT_LINE, PUTF
• Procedure NEW_LINE
•
Procedure FFLUSH• Procedure FCLOSE, FCLOSE_ALL
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 5/18
Exceptions Specific to the
UTL_FILE Package• INVALID_PATH
• INVALID_MODE
• INVALID_FILEHANDLE• INVALID_OPERATION
• READ_ERROR
•
WRITE_ERROR• INTERNAL_ERROR
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 6/18
FUNCTION FOPEN
(location IN VARCHAR2,
filename IN VARCHAR2,
open_mode IN VARCHAR2)RETURN UTL_FILE.FILE_TYPE;
FUNCTION IS_OPEN
(file_handle IN FILE_TYPE)
RETURN BOOLEAN;
The FOPEN and IS_OPEN
Functions
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 7/18
CREATE OR REPLACE PROCEDURE sal_status
(p_filedir IN VARCHAR2, p_filename IN VARCHAR2)
IS
v_filehandle UTL_FILE.FILE_TYPE;
CURSOR emp_info IS
SELECT last_name, salary, department_id
FROM employeesORDER BY department_id;
v_newdeptno employees.department_id%TYPE;
v_olddeptno employees.department_id%TYPE := 0;
BEGIN
v_filehandle := UTL_FILE.FOPEN (p_filedir, p_filename,'w');
UTL_FILE.PUTF (v_filehandle,'SALARY REPORT: GENERATED ON%s\n', SYSDATE);
UTL_FILE.NEW_LINE (v_filehandle);
FOR v_emp_rec IN emp_info LOOP
v_newdeptno := v_emp_rec.department_id;
...
Using UTL_FILE sal_status.sql
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 8/18
...IF v_newdeptno <> v_olddeptno THEN
UTL_FILE.PUTF (v_filehandle, 'DEPARTMENT: %s\n',
v_emp_rec.department_id);
END IF;
UTL_FILE.PUTF (v_filehandle,' EMPLOYEE: %s earns: %s\n',
v_emp_rec.last_name, v_emp_rec.salary);v_olddeptno := v_newdeptno;
END LOOP;
UTL_FILE.PUT_LINE (v_filehandle, '*** END OF REPORT ***');
UTL_FILE.FCLOSE (v_filehandle);
EXCEPTION
WHEN UTL_FILE.INVALID_FILEHANDLE THEN
RAISE_APPLICATION_ERROR (-20001, 'Invalid File.');
WHEN UTL_FILE.WRITE_ERROR THEN
RAISE_APPLICATION_ERROR (-20002, 'Unable to write tofile');
END sal_status;/
Using UTL_FILE sal_status.sql
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 9/18
External Tables
• External tables are read-only tables in which thedata is stored outside the database in flat files.
• The metadata for an external table is createdusing a CREATE TABLE statement.
• With the help of external tables, Oracle data canbe stored or unloaded as flat files.
• The data can be queried using SQL, but you cannotuse DML and no indexes can be created.
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 10/18
Creating an External Table• Use the external_table_clause along with the
CREATE TABLE syntax to create an external table.
• Specify ORGANIZATION as EXTERNAL to indicate that
the table is located outside the database.
•
The external_table_clause consists of theaccess driver TYPE, external_data_properties,and the REJECT LIMIT.
• The external_data_properties consist of the
following:– DEFAULT DIRECTORY
– ACCESS PARAMETERS
– LOCATION
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 11/18
Example of Creating an
External TableCreate a DIRECTORY object that
corresponds to the directory on the file
system where the external data sourceresides.
CREATE DIRECTORY emp_dir AS '/flat_files' ;
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 12/18
Example of Creating an
External TableCREATE TABLE oldemp (
empno NUMBER, empname CHAR(20), birthdate DATE)ORGANIZATION EXTERNAL(TYPE ORACLE_LOADERDEFAULT DIRECTORY emp_dir
ACCESS PARAMETERS(RECORDS DELIMITED BY NEWLINE
BADFILE 'bad_emp'LOGFILE 'log_emp'FIELDS TERMINATED BY ','(empno CHAR,empname CHAR,
birthdate CHAR date_format date mask "dd-mon-yyyy"))
LOCATION ('emp1.txt'))PARALLEL 5REJECT LIMIT 200;
Table created.
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 13/18
Querying External Tables
SELECT *FROM oldemp
emp1.txt
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 14/18
What is SQL*Loader and what is it
used for?
• SQL*Loader is a bulk loader utility used formoving data from external files into the Oracledatabase.
• SQL*Loader supports various load formats,selective loading, and multi-table loads
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 15/18
How does one use the SQL*Loader
utility?
• One can load data into an Oracle database byusing the sqlldr (sqlload on some platforms)utility. Invoke the utility without arguments to
get a list of available parameters. Look at thefollowing example:
• sqlldr scott/tiger control=loader.ctl
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 16/18
Data File
• The mydata.txt file may look like this:
• 10001,"Scott Tiger", 1000, 40
•
10002,"Frank Naude", 500, 20
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 17/18
Control File
load data
infile 'c:\data\mydata.csv'
into table empfields terminated by "," optionally enclosed by '"'
( empno, empname, sal, deptno )
8/12/2019 Flat File Access
http://slidepdf.com/reader/full/flat-file-access 18/18
• load data• infile *• replace• into table departments
• ( dept position (02:05) char(4),• deptname position (08:27) char(20)• )• begindata•
COSC COMPUTER SCIENCE• ENGL ENGLISH LITERATURE• MATH MATHEMATICS• POLY POLITICAL SCIENCE
Control File with in-line data