Sunteți pe pagina 1din 18

Accessing Flat Files

Three Different Ways


UTL_FILE Oracle Loader SQL Loader

What Is the UTL_FILE Package?


Extends I/O to text files within PL/SQL Provides security for directories on the server through 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

File Processing Using the UTL_FILE Package

Open the text file

Get lines from the text file Put lines into the text file

Yes More No lines to process? Close the text file

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

Exceptions Specific to the UTL_FILE Package


INVALID_PATH INVALID_MODE INVALID_FILEHANDLE INVALID_OPERATION READ_ERROR WRITE_ERROR INTERNAL_ERROR

The FOPEN and IS_OPEN Functions


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;

sal_status.sql

Using UTL_FILE

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 employees ORDER 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; ...

sal_status.sql

Using UTL_FILE

... 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 to file'); END sal_status; /

External Tables
External tables are read-only tables in which the data is stored outside the database in flat files. The metadata for an external table is created using a CREATE TABLE statement. With the help of external tables, Oracle data can be stored or unloaded as flat files. The data can be queried using SQL, but you cannot use DML and no indexes can be created.

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 the access driver TYPE, external_data_properties, and the REJECT LIMIT. The external_data_properties consist of the following: DEFAULT DIRECTORY ACCESS PARAMETERS LOCATION

Example of Creating an External Table


Create a DIRECTORY object that corresponds to the directory on the file system where the external data source resides.
CREATE DIRECTORY emp_dir AS '/flat_files' ;

Example of Creating an CREATE TABLE oldemp ( External Table empno NUMBER, empname CHAR(20), birthdate DATE)
ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT 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 5 REJECT LIMIT 200; Table created.

Querying External Tables


SELECT * FROM oldemp

emp1.txt

What is SQL*Loader and what is it used for?


SQL*Loader is a bulk loader utility used for moving data from external files into the Oracle database. SQL*Loader supports various load formats, selective loading, and multi-table loads

How does one use the SQL*Loader utility?


One can load data into an Oracle database by using the sqlldr (sqlload on some platforms) utility. Invoke the utility without arguments to get a list of available parameters. Look at the following example: sqlldr scott/tiger control=loader.ctl

Data File
The mydata.txt file may look like this: 10001,"Scott Tiger", 1000, 40 10002,"Frank Naude", 500, 20

Control File
load data infile 'c:\data\mydata.csv' into table emp fields terminated by "," optionally enclosed by '"' ( empno, empname, sal, deptno )

Control File with in-line data


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

S-ar putea să vă placă și