Home > Ask the Oracle Database / Applications Experts > Questions & Answers > Using PL/SQL to read delimited ASCII text files into tables
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Using PL/SQL to read delimited ASCII text files into tables

Frank Kulash EXPERT RESPONSE FROM: Frank Kulash

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 22 July 2003
I have an ASCII text file delimited by "|" characters. What is the best of reading these records into tables using PL/SQL?


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary


Let me begin by answering a related question, a question very similar to yours. In fact, this question is yours with only one character added:

I have an ASCII text file delimited by "|" characters. What is the best of reading these records into tables: using PL/SQL?

No, the best way of reading those files is to use SQL*Loader. For all its quirks and poor documentation, SQL*Loader is still the best tool for this job. Trying to read the file in PL/SQL is somewhat like trying to open a can of peas with a hammer: it's possible to do it, but why would you ever want to? You'd be much better off getting a package of fresh or frozen peas and opening that with a hammer.

To use SQL*Loader, you have to write a control file that will describe the input fields and link them to database tables and columns. The control file also specifies various options, like what to do with records that have errors.

To load this file, question.csv:

Question|Answer|Category|Date Submitted|ID
"How can I read a text file with ""|"" characters between fields?"|Use SQLLoader|PLSQL|15-Jul-2003|1
Why do canned peas taste so bad?||MISC|15-Jul-2003|2
into this table:
CREATE TABLE    question
(
  question_id   NUMBER (8)      PRIMARY KEY,
  question_text VARCHAR2 (200)  NOT NULL,
  answer_text   VARCHAR2 (4000),
  status_code   CHAR (1),
  submit_dt     DATE
);
you can use this control file, question.ldr:
-- Question.ldr Load Question table
-- 18 July 2003 -- Script written.  (Frank Kulash)
--
OPTIONS
(
-- The first line in the file is a header: ignore it
 SKIP = 1
)
LOAD DATA
INFILE  'question.csv'
-- Rejected records will be written to BADFILE 
BADFILE  'question.bad'
-- APPEND means add to existing table,
-- REPLACE means truncate table first
APPEND
INTO TABLE question
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(
  question_text,
  answer_text     CHAR (4000),
  category_abbr   FILLER,
  submit_dt       DATE         'DD-Mon-YYYY',
  question_id
)
You can run SQL*Loader from an operating-system command prompt like this:
SQLLDR  USERID=scott/tiger@conect_string
  CONTROL=question.ldr  LOG=question.log

(The command above is really one long line.) Note these features:
  • SQL*Loader reads the table description from the database, so you don't have to give type specifications for most numbers or short strings.
  • Specify CHAR (len) for string columns over 250 characters long
  • Specify FILLER to skip a column in the data file
  • Database columns not specified will be set to NULL
SQL*Loader can also generate data (id numbers, constants, etc.). A few examples:
  modify_user_name  CONSTANT  'SCOTT',
  modify_dt_tm      SYSDATE,
  id                SEQUENCE (MAX)

Now to answer your question: Next page...




Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Oracle White Papers: Fusion Middleware
HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2003 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts