SlideShare a Scribd company logo
1 of 18
Download to read offline
Oracle10g External Files
                An introduction to what is
                External Tables?




3/4/2010        For a Better Future          1
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
Topics of Discussion
              History of External File
               usage?
              Reading External Tables?
              Writing External Tables?



3/4/2010           For a Better Future    3
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
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
External Table
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
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
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
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
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
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
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
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
Write to External Files




3/4/2010             For a Better Future   15
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
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
Thank You
              Q&A?




3/4/2010              For a Better Future   18

More Related Content

What's hot

BCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfBCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdf
KeerthanaP37
 

What's hot (19)

BCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfBCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdf
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
How sqlite works
How sqlite worksHow sqlite works
How sqlite works
 
Tables in dd
Tables in ddTables in dd
Tables in dd
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Does Your IBM i Security Meet the Bar for GDPR?
Does Your IBM i Security Meet the Bar for GDPR?Does Your IBM i Security Meet the Bar for GDPR?
Does Your IBM i Security Meet the Bar for GDPR?
 
Data Warehouse and Business Intelligence - Recipe 1
Data Warehouse and Business Intelligence - Recipe 1Data Warehouse and Business Intelligence - Recipe 1
Data Warehouse and Business Intelligence - Recipe 1
 
Dictionary.coumns is your friend while appending or moving data
Dictionary.coumns is your friend while appending or moving dataDictionary.coumns is your friend while appending or moving data
Dictionary.coumns is your friend while appending or moving data
 
NOTES ON "FOXPRO"
NOTES ON "FOXPRO" NOTES ON "FOXPRO"
NOTES ON "FOXPRO"
 
SQL
SQLSQL
SQL
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
dbms lab manual
dbms lab manualdbms lab manual
dbms lab manual
 
Sql
SqlSql
Sql
 
Sql injection
Sql injectionSql injection
Sql injection
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 
SQL Tutorial for BCA-2
SQL Tutorial for BCA-2SQL Tutorial for BCA-2
SQL Tutorial for BCA-2
 
CIS 336 STUDY Become Exceptional--cis336study.com
CIS 336 STUDY Become Exceptional--cis336study.comCIS 336 STUDY Become Exceptional--cis336study.com
CIS 336 STUDY Become Exceptional--cis336study.com
 
CIS 336 STUDY Achievement Education--cis336study.com
CIS 336 STUDY Achievement Education--cis336study.comCIS 336 STUDY Achievement Education--cis336study.com
CIS 336 STUDY Achievement Education--cis336study.com
 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 

Viewers also liked (6)

CHX PYTHON INTRO
CHX PYTHON INTROCHX PYTHON INTRO
CHX PYTHON INTRO
 
What is Listagg?
What is Listagg?What is Listagg?
What is Listagg?
 
PL/SQL11g Question #1
PL/SQL11g Question #1PL/SQL11g Question #1
PL/SQL11g Question #1
 
SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning Overview
 
Sql Question #3
Sql Question #3Sql Question #3
Sql Question #3
 
Sql Question #5
Sql Question #5Sql Question #5
Sql Question #5
 

Similar to Oracle10g External Tables

Oracle External Tables
Oracle External TablesOracle External Tables
Oracle External Tables
grogers1124
 
Oracle External Tables
Oracle External TablesOracle External Tables
Oracle External Tables
grogers1124
 
Practicas oracle10g
Practicas oracle10gPracticas oracle10g
Practicas oracle10g
rehoscript
 
Dba 3+ exp qus
Dba 3+ exp qusDba 3+ exp qus
Dba 3+ exp qus
krreddy21
 
aotc_120805_mwagner
aotc_120805_mwagneraotc_120805_mwagner
aotc_120805_mwagner
Mary Wagner
 
How to create a non managed standby database
How to create a non managed  standby databaseHow to create a non managed  standby database
How to create a non managed standby database
Jorge Batista
 
Create manula and automaticly database
Create manula and automaticly databaseCreate manula and automaticly database
Create manula and automaticly database
Anar Godjaev
 

Similar to Oracle10g External Tables (20)

MySQL Performance Tuning: The Perfect Scalability (OOW2019)
MySQL Performance Tuning: The Perfect Scalability (OOW2019)MySQL Performance Tuning: The Perfect Scalability (OOW2019)
MySQL Performance Tuning: The Perfect Scalability (OOW2019)
 
Changing platforms of Oracle database
Changing platforms of Oracle databaseChanging platforms of Oracle database
Changing platforms of Oracle database
 
Oracle External Tables
Oracle External TablesOracle External Tables
Oracle External Tables
 
Oracle External Tables
Oracle External TablesOracle External Tables
Oracle External Tables
 
Practicas oracle10g
Practicas oracle10gPracticas oracle10g
Practicas oracle10g
 
Etl2
Etl2Etl2
Etl2
 
XMLDB Building Blocks And Best Practices - Oracle Open World 2008 - Marco Gra...
XMLDB Building Blocks And Best Practices - Oracle Open World 2008 - Marco Gra...XMLDB Building Blocks And Best Practices - Oracle Open World 2008 - Marco Gra...
XMLDB Building Blocks And Best Practices - Oracle Open World 2008 - Marco Gra...
 
Dba 3+ exp qus
Dba 3+ exp qusDba 3+ exp qus
Dba 3+ exp qus
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAsOracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Oracle Database 12c - New Features for Developers and DBAs
Oracle Database 12c  - New Features for Developers and DBAsOracle Database 12c  - New Features for Developers and DBAs
Oracle Database 12c - New Features for Developers and DBAs
 
Sql introduction
Sql introductionSql introduction
Sql introduction
 
Database.pptx
Database.pptxDatabase.pptx
Database.pptx
 
aotc_120805_mwagner
aotc_120805_mwagneraotc_120805_mwagner
aotc_120805_mwagner
 
How to create a non managed standby database
How to create a non managed  standby databaseHow to create a non managed  standby database
How to create a non managed standby database
 
Big Data with MySQL
Big Data with MySQLBig Data with MySQL
Big Data with MySQL
 
2008 Collaborate IOUG Presentation
2008 Collaborate IOUG Presentation2008 Collaborate IOUG Presentation
2008 Collaborate IOUG Presentation
 
database backup and recovery
database backup and recoverydatabase backup and recovery
database backup and recovery
 
Oracle RDBMS architecture
Oracle RDBMS architectureOracle RDBMS architecture
Oracle RDBMS architecture
 
Create manula and automaticly database
Create manula and automaticly databaseCreate manula and automaticly database
Create manula and automaticly database
 
576 oracle-dba-interview-questions
576 oracle-dba-interview-questions576 oracle-dba-interview-questions
576 oracle-dba-interview-questions
 

More from Kai Liu (15)

QQ and Advance query
QQ and Advance queryQQ and Advance query
QQ and Advance query
 
Schedulers
SchedulersSchedulers
Schedulers
 
PL/SQL Example for IF .. ELSIF
PL/SQL Example for IF .. ELSIFPL/SQL Example for IF .. ELSIF
PL/SQL Example for IF .. ELSIF
 
Dms 2.0 Direction
Dms 2.0 DirectionDms 2.0 Direction
Dms 2.0 Direction
 
Dms 2.0 Plan Proposal
Dms 2.0 Plan ProposalDms 2.0 Plan Proposal
Dms 2.0 Plan Proposal
 
Toad
ToadToad
Toad
 
Dms Emailing Reports
Dms Emailing ReportsDms Emailing Reports
Dms Emailing Reports
 
QQ And Advance Query
QQ And Advance QueryQQ And Advance Query
QQ And Advance Query
 
Troubleshooting Batch Reports
Troubleshooting Batch ReportsTroubleshooting Batch Reports
Troubleshooting Batch Reports
 
Dms Reporting Criteria
Dms Reporting CriteriaDms Reporting Criteria
Dms Reporting Criteria
 
Dms Batch Reporting
Dms Batch ReportingDms Batch Reporting
Dms Batch Reporting
 
Dms Reporting Overview
Dms Reporting OverviewDms Reporting Overview
Dms Reporting Overview
 
Dms 2 Direction
Dms 2 DirectionDms 2 Direction
Dms 2 Direction
 
Dms Project
Dms ProjectDms Project
Dms Project
 
Dms Acknowledgements
Dms AcknowledgementsDms Acknowledgements
Dms Acknowledgements
 

Recently uploaded

Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
panagenda
 

Recently uploaded (20)

Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Portal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russePortal Kombat : extension du réseau de propagande russe
Portal Kombat : extension du réseau de propagande russe
 
Microsoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - QuestionnaireMicrosoft CSP Briefing Pre-Engagement - Questionnaire
Microsoft CSP Briefing Pre-Engagement - Questionnaire
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Oauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoftOauth 2.0 Introduction and Flows with MuleSoft
Oauth 2.0 Introduction and Flows with MuleSoft
 

Oracle10g External Tables

  • 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
  • 15. Write to External Files 3/4/2010 For a Better Future 15
  • 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