Exforsys

Home arrow Reviews arrow Oracle Database Management Utilities

Oracle Utilities - Creating External Table Metadata, The Easy Way

Author: Packt Publishing     Published on: 4th Jan 2010

To further illustrate the tight relationship between SQL*Loader and External Tables, the SQL*Loader tool may be used to generate a script that creates an External Table according to a pre-existing control file.

 

Ads

SQL*Loader has a command line option named EXTERNAL_TABLE, this can hold one of three different parameters {NOT_USED | GENERATE_ONLY | EXECUTE}. If nothing is set, it defaults to the NOT_USED option.

This keyword is used to generate the script to create an External Table, and the options mean: · NOT_USED: This is the default option, and it means that no External Tables are to be used in this load.

· GENERATE_ONLY: If this option is specified, then SQL*Loader will only read the definitions from the control file and generate the required commands, so the user can record them for later execution, or tailor them to fit his/her particular needs.

· EXECUTE: This not only generates the External Table scripts, but also executes them. If the user requires a sequence, then the EXECUTE option will not only create the table, but it will also create the required sequence, deleting it once the data load is finished. This option performs the data load process against the specified target regular by means of an External Table, it creates both the directory and the External Table, and inserts the data using a SELECT AS INSERT with the APPEND hint.

Let's use the GENERATE_ONLY option to generate the External Table creation scripts:

$ sqlldr exttabdemo/oracle employees external_table=GENERATE_ONLY


By default the log file is located in a file whose extension is .log and its name equals that of the control file. By opening it we see, among the whole log processing information, this set of DDL commands:


Sample Code
  1.  
  2.    CREATE TABLE "SYS_SQLLDR_X_EXT_EMPLOYEES"
  3.    (
  4.      "EMPLOYEE_ID" NUMBER(6),
  5.      "FIRST_NAME" VARCHAR2(20),
  6.      "LAST_NAME" VARCHAR2(25),
  7.      "EMAIL" VARCHAR2(25),
  8.      "PHONE_NUMBER" VARCHAR2(20),
  9.      "HIRE_DATE" DATE,
  10.      "JOB_ID" VARCHAR2(10),
  11.      "SALARY" NUMBER(8,2),
  12.      "COMMISSION_PCT" NUMBER(2,2),
  13.      "MANAGER_ID" NUMBER(6),
  14.      "DEPARTMENT_ID" NUMBER(4)
  15.    )
  16.    ORGANIZATION external
  17.    (
  18.      TYPE oracle_loader
  19.      DEFAULT DIRECTORY EXTTABDIR
  20.      ACCESS PARAMETERS
  21.      (
  22.        RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII
  23.        BADFILE 'EXTTABDIR':'employees.bad'
  24.        LOGFILE 'employees.log_xt'
  25.        READSIZE 1048576
  26.        FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' LDRTRIM
  27.  
  28.           REJECT ROWS WITH ALL NULL FIELDS
  29.           (
  30.             "EMPLOYEE_ID" CHAR(255)
  31.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  32.             "FIRST_NAME" CHAR(255)
  33.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  34.             "LAST_NAME" CHAR(255)
  35.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  36.             "EMAIL" CHAR(255)
  37.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  38.             "PHONE_NUMBER" CHAR(255)
  39.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  40.             "HIRE_DATE" CHAR(255)
  41.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  42.             "JOB_ID" CHAR(255)
  43.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  44.             "SALARY" CHAR(255)
  45.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  46.             "COMMISSION_PCT" CHAR(255)
  47.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  48.             "MANAGER_ID" CHAR(255)
  49.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"',
  50.             "DEPARTMENT_ID" CHAR(255)
  51.               TERMINATED BY "," OPTIONALLY          ENCLOSED BY '"'
  52.           )
  53.         )
  54.         location
  55.         (
  56.           'employees.txt'
  57.         )
  58.     )
  59.  
Copyright exforsys.com



The more complete version is shown, some differences with the basic script are:

· All the column definitions are set to CHAR(255) with the delimiter character defined for each column
· If the current working directory is already registered as a regular DIRECTORY at the database level, SQL*Loader utilizes it, otherwise, it creates a new directory definition
· The script specifies where the bad files and log file are located
· It specifies that an all-null column record is rejected

In the case of the EXECUTE keyword, the log file shows that not only are the scripts used to create the External Table, but also to execute the INSERT statement with the /*+ append */ hint. The load is performed in direct path mode.

All External Tables, when accessed, generate a log file. In the case of the ORACLE_LOADER driver, this file is similar to the file generated by SQL*Loader. It has a different format in the case of ORACLE_DATAPUMP driver. The log file is generated in the same location where the external file resides, and its format is as follows:

_.log

When an ORACLE_LOADER managed External Table has errors, it dumps the 'bad' rows to the *.bad file, just the same as if this was loaded by SQL*Loader.

 

Ads

The ORACLE_DATAPUMP External Table generates a simpler log file, it only contains the time stamp when the External Table was accessed, and it creates a log file for each oracle process accessing the External Table.



 
This tutorial is part of a Oracle Database Management Utilities tutorial series. Read it from the beginning and learn yourself.

Oracle Database Management Utilities

 

Comments