1. Oracle10g External Files
An introduction to what is
External Tables?
3/4/2010 For a Better Future 1
2. Introduction
To have a better understanding of
how External Table working in
database.
By Kai Liu, Sr. Oracle Developer
Kaitech, Inc.
3/4/2010 For a Better Future 2
3. Topics of Discussion
History of External File
usage?
Reading External Tables?
Writing External Tables?
3/4/2010 For a Better Future 3
4. History of External
File usage?
Previously the main method of getting external data into
the database
Usually a disjointed process
– File is put into a directory
– Loader is run to load into temporary table
– Validation is done on loaded rows
Problems?
– Data File not found
– Bad Format – rows not loaded
– Have to load into character fields then validate to
avoid bad formats
– Separate steps mean coordination if first step fails or
does not load all
3/4/2010 For a Better Future 4
5. Control File
options (errors=200,direct=true)
load data
replace
into table TEMP_DAILY_ORDER_HEADERS
WHEN REC_TYPE = ‘OH’
(
CUST_ID POSITION(001:008) INTEGER EXTERNAL,
ORDER_ID POSITION(009:016) INTEGER EXTERNAL,
ORDER_DATE POSITION(017:024) DATE FORMAT ‘YYYYMMDD’,
NUM_DETAILS POSITION(025:030) INTEGER EXTERNAL,
REC_TYPE POSITION(079:080) CHAR)
into table TEMP_DAILY_ORDER_DETAILS
WHEN REC_TYPE = ‘OD’
(
CUST_ID POSITION(001:008) INTEGER EXTERNAL,
ORDER_ID POSITION(009:016) INTEGER EXTERNAL,
LINE_NUM POSITION(017:020) INTEGER EXTERNAL,
PRODUCT_ID POSITION(021:028) CHAR,
QUANTITY POSITION(029:033) INTEGER EXTERNAL,
PRICE POSITION(034:041) DECIMAL EXTERNAL,
AMOUNT POSITION(042:050) DECIMAL EXTERNAL,
DISCOUNT POSITION(051:057) DECIMAL EXTERNAL,
NET_AMOUNT POSITION(058:066) DECIMAL EXTERNAL,
REC_TYPE POSITION(079:080) CHAR)
3/4/2010 For a Better Future 5
7. Reading External Tables?
Started with Oracle 9i
• External Files referenced directly from the
database, as if they were tables
• Uses ORACLE_LOADER driver
• Define the file Directories
• Tables are created with basic file format
• SELECT used as if file is another table
• Cannot INSERT, UPDATE or DELETE
Let’s Demo
3/4/2010 For a Better Future 7
8. Define Directory
Create directory where external data files are located
– files must be in this area
create directory external_dir as 'c:dataoracleextapp';
create directory external_bad_dir as
'c:dataoracleextapp';
Example in Unix:
DROP DIRECTORY KMSDATA;
CREATE OR REPLACE DIRECTORY
KMSDATA AS '/home/oracle/data';
3/4/2010 For a Better Future 8
9. Create the Table Definitions
Create table TEMP_PUBLICITY_NAME
(
ACCT_NBR VARCHAR2(8),
PUBLICITY_NAME1 VARCHAR2(30),
PUBLICITY_NAME2 VARCHAR2(30)
)
ORGANIZATION EXTERNAL
(TYPE ORACLE_LOADER
DEFAULT DIRECTORY dmsdata
ACCESS PARAMETERS
(
RECORDS DELIMITED BY newline
BADFILE 'publname.bad'
DISCARDFILE 'publname.dis'
LOGFILE 'publname.log'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '~'
(ACCT_NBR CHAR,
PUBLICITY_NAME1 CHAR,
PUBLICITY_NAME2 CHAR)
)
LOCATION ('PUBLNAME.txt')
)
REJECT LIMIT UNLIMITED;
/
3/4/2010 For a Better Future 9
10. Sample Data File
~00002136~,~JANE DOE~,~AND PAPER INCORPORATED~
~00017012~,~JOHN AND MARY~,~~
~00017013~,~JOHN AND MARY~,~~
~00043376~,~MRS MILLS~,~DO NOT ASK FOR PLEDGE-COVERED~
~00043833~,~MRS NANCY~,~DO NOT ASK FOR PLEDGE-COVERED~
~00051155~,~MRS JAMES~,~DO NOT ASK FOR PLEDGE-COVERED~
~00051812~,~MRS WENDY~,~~
~00057145~,~MRS CHARLES~,~DO NOT ASK FOR PLEDGE-COVERED~
~00081234~,~HARRY~,~(MR STUART L PINKERT)~
~06724967~,~MRS CAROL ~,~DO NOT SOLICIT EVER~
~14660280~,~BRADEY FAMILY~,~(MR & MRS SMITH)~
3/4/2010 For a Better Future 10
11. Access the table
Access the table with SQL
SQL> select * from temp_publicity_name;
ACCT_NBR PUBLICITY_NAME1 PUBLICITY_NAME2
-------- ------------------------------ ------------------------------
00002136 JANE DOE AND PAPER INCORPORATED
00017012 JOHN & MARY
00017012 JOHN & MARY
00043376 MRS MILLS DO NOT ASK FOR
00043833 MRS NANCY DO NOT ASK FOR
00051155 MRS JAMES DO NOT ASK FOR
00051812 MRS WENDY
00057145 MRS CHARLES DO NOT ASK FOR
00081234 HARRY (MR STUART)
06724967 MRS CAROL DO NOT EVER
14660280 BRADY FAMILY (MR & MRS SMITH)
You join good table with these Internal Table.
3/4/2010 For a Better Future 11
12. Validate the table in PL/SQL
Invalid format errors automatically go to the
BAD file
• How to validate the data?
1. Access the table and validate records
that fail
1. Define bad file as external table with
character records
2. Use PL/SQL to validate the bad records
2. Define the external table with character
fields and validate the records
before loading
3/4/2010 For a Better Future 12
13. Check it Out!
Check if the file exists:
– File Name is missing from the directory
– Rename custdata.csv to custdata_mistake.csv
SQL> select count(*) from temp_publicity_name;
select count(*) from temp_publicity_name
*
ERROR at line 1:
ORA-29913: error in executing ODCIEXTTABLEOPEN
callout
ORA-29400: data cartridge error
KUP-04040: file PUBLNAME.txt in DMSDATA not found
ORA-06512: at "SYS.ORACLE_LOADER", line 19–
Rename file back to PUBLNAME.txt
SQL> select count(*) from temp_publicity_name;
COUNT(*)
----------
25
– Can Use Exception Handling!
3/4/2010 For a Better Future 13
14. Writing External Tables
• New in Oracle 10g
• Capability to write to an external
table
• Oracle-proprietary format
• Can only be read by Oracle DB
• Looks like an Export file
• Can be created, then read
• Uses ORACLE_DATAPUMP driver
3/4/2010 For a Better Future 14
16. External Tables
• External tables enable an
application to import data from files
and create output files for transfer
to another Oracle database
• Easier to follow SQL code than
Loader control files
3/4/2010 For a Better Future 16
17. Conclusion
• External tables in 10g – if you are
not using them, now is the time to
start!
• Loader is still around in 10g, but
should no longer be used!
• I still find massive amounts of
SQL*Loader code in our current
applications!
• We should be using this now!
3/4/2010 For a Better Future 17
18. Thank You
Q&A?
3/4/2010 For a Better Future 18