Generate external table definitions from SQL*Loader
Takeaway: External tables in Oracle enable you to query text files with the full power of the SQL language. Here is a quick way to create them using scripts generated by SQL*Loader.
External tables, introduced in Oracle 9i, enable you to map a sequential text file to an Oracle table definition. Once the external table is defined, you can use the full power of the SQL SELECT statement—including parallel querying—to process it.
External tables are based on SQL*Loader, but the syntax to define them is different. If you're already familiar with SQL*Loader, there is a shortcut: use SQL*Loader itself to generate a script that sets up the external table definition for you.
The parameter EXTERNAL_TABLE=GENERATE_ONLY, when added to a SQL*Loader command line, will translate the control file used in the run into a SQL script and output it to the log file of the run. You can edit the log file or copy and paste the code into a new script.
Listing A shows a SQL*Loader control file designed to load variable-length text records into the database. Executing the following command does not cause rows to be loaded, but it does output an external table script to the log file. Listing B shows that output.
sqlldr scott/tiger CONTROL=loademp.ctl EXTERNAL_TABLE=GENERATE_ONLY
First, the script creates a directory object that points to the directory containing the input file. Directory objects give short, platform-neutral names to operating system-specific full pathnames. The CREATE DIRECTORY command makes the association. You'll need the CREATE ANY DIRECTORY system privilege to do this, or have a DBA run it for you and grant permissions. Also, the system-generated name is awkward; it's a good idea to edit the script to give it a more meaningful name before you run it.
Next, the script contains the CREATE TABLE statement itself. The statement references the directory object (edit this if you changed the name above), the file to be loaded, and the ORGANIZATION EXTERNAL clause. It also contains the syntax that describes the text file's layout.
Finally, the script contains an example INSERT statement that SELECTS from the external table in a subquery, and statements to drop both the external table and directory when you are done with them.
Miss a tip?
Check out the Oracle archive, and catch up on our most recent Oracle tips.
Bob Watkins (OCP, MCDBA, MCSE, MCT) is a computer professional with 25 years of experience as a technical trainer, consultant, and database administrator. Visit Bob's site.
Print/View all Posts Comments on this article
SponsoredWhite Papers, Webcasts, and Downloads
- 7 Things Every System Administrator Should Know About OpenSSH Global Knowledge
- Vista SP1: What You Need To Know Before You Deploy Global Knowledge
- Eleven Myths about 802.11 Wi-Fi Networks Global Knowledge
- Using the Six Laws of Persuasion in Negotiations Global Knowledge
- ITIL Version 3.0 -- What It Means to You Global Knowledge
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
