Exforsys.com
 
Home Tutorials Oracle Utilities
 

Oracle Utilities - Data Transformation with External Tables

 

One of the main uses of the External Tables is their support of the ETL process, allowing the user to perform a data load that is transformed to the target format without an intermediate stage table.


 



Let's read an External Table whose contents are:


This data can be loaded in a single command to multiple tables. Let's create several tables with the same structure:


Sample Code
  1.  
  2. SQL> desc amount_jan
  3.  Name              Null?         Type
  4.  ----------------- --------      ------------
  5.  REGION                          VARCHAR2(16)
  6.  AMOUNT                          NUMBER(3)
  7.  
Copyright exforsys.com


 


Now we can issue a command to send the data from the External Table to the different tables.


Sample Code
  1.  
  2.    INSERT ALL
  3.       INTO AMOUNT_JAN (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  4.       INTO AMOUNT_FEB (REGION,       AMOUNT)      VALUES(COUNTRY,   FEB)
  5.       INTO AMOUNT_MAR (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  6.       INTO AMOUNT_APR (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  7.       INTO AMOUNT_MAY (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  8.       INTO AMOUNT_JUN (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  9.       INTO AMOUNT_JUL (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  10.       INTO AMOUNT_AUG (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  11.       INTO AMOUNT_SEP (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  12.       INTO AMOUNT_OCT (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  13.       INTO AMOUNT_NOV (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  14.       INTO AMOUNT_DEC (REGION,       AMOUNT)      VALUES(COUNTRY,   JAN)
  15.    SELECT     COUNTRY,
  16.       JAN,
  17.       FEB,
  18.       MAR,
  19.       APR,
  20.       MAY,
  21.       JUN,
  22.       JUL,
  23.       AUG,
  24.       SEP,
  25.       OCT,
  26.       NOV,
  27.       DEC
  28.    FROM       REGION_REVENUE
  29.  
Copyright exforsys.com


In this example, we will perform a conditional insert to different tables depending on the value of the amount column. We will first create three tables, one for low, another for average, and a third for high amounts:


Sample Code
  1.  
  2. SQL> create table low_amount(
  3.   2 region      varchar2(16),
  4.   3 month       number(2),
  5.   4 amount      number(3))
  6. Table created.
  7. SQL> create table high_amount as select * from low_amount
  8. Table created.
  9.  
Copyright exforsys.com


Now we can read the External Table and have the data inserted conditionally to one of three mutually exclusive targets.


Sample Code
  1.  
  2.     INSERT ALL
  3.             WHEN ( JAN <= 500 ) THEN
  4.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  5.                     VALUES ( COUNTRY, '01', JAN )
  6.             WHEN ( FEB <= 500 ) THEN
  7.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  8.                     VALUES ( COUNTRY, '02', FEB )
  9.             WHEN ( MAR <= 500 ) THEN
  10.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  11.                     VALUES ( COUNTRY, '03', MAR )
  12.             WHEN ( APR <= 500 ) THEN
  13.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  14.                     VALUES ( COUNTRY, '04', APR )
  15.             WHEN ( MAY <= 500 ) THEN
  16.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  17.                     VALUES ( COUNTRY, '05', MAY )
  18.             WHEN ( JUN <= 500 ) THEN
  19.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  20.                     VALUES ( COUNTRY, '06', JUN )
  21.             WHEN ( JUL <= 500 ) THEN
  22.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  23.                     VALUES ( COUNTRY, '07', JUL )
  24.             WHEN ( AUG <= 500 ) THEN
  25.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  26.                     VALUES ( COUNTRY, '08', AUG )
  27.             WHEN ( SEP <= 500 ) THEN
  28.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  29.                     VALUES ( COUNTRY, '09', SEP )
  30.             WHEN ( OCT <= 500 ) THEN
  31.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  32.                     VALUES ( COUNTRY, '10', OCT )
  33.             WHEN ( NOV <= 500 ) THEN
  34.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  35.                     VALUES ( COUNTRY, '11', NOV )
  36.             WHEN ( DEC <= 500 ) THEN
  37.                     INTO LOW_AMOUNT( REGION, MONTH, AMOUNT)
  38.                     VALUES ( COUNTRY, '12', DEC )
  39.             WHEN ( JAN > 500 ) THEN
  40.                     INTO HIGH_AMOUNT( REGION, MONTH, AMOUNT)
  41.                     VALUES ( COUNTRY, '01', JAN )
  42.             WHEN ( FEB > 500 ) THEN
  43.                     INTO HIGH_AMOUNT( REGION, MONTH, AMOUNT)
  44.                     VALUES ( COUNTRY, '02', FEB )
  45.             WHEN ( MAR > 500 ) THEN
  46.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  47.                   VALUES ( COUNTRY,   '03', MAR )
  48.          WHEN ( APR > 500 ) THEN
  49.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  50.                   VALUES ( COUNTRY,   '04', APR )
  51.          WHEN ( MAY > 500 ) THEN
  52.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  53.                   VALUES ( COUNTRY,   '05', MAY )
  54.          WHEN ( JUN > 500 ) THEN
  55.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  56.                   VALUES ( COUNTRY,   '06', JUN )
  57.          WHEN ( JUL > 500 ) THEN
  58.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  59.                   VALUES ( COUNTRY,   '07', JUL )
  60.          WHEN ( AUG > 500 ) THEN
  61.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  62.                   VALUES ( COUNTRY,   '08', AUG )
  63.          WHEN ( SEP > 500 ) THEN
  64.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  65.                   VALUES ( COUNTRY,   '09', SEP )
  66.          WHEN ( OCT > 500 ) THEN
  67.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  68.                   VALUES ( COUNTRY,   '10', OCT )
  69.          WHEN ( NOV > 500 ) THEN
  70.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  71.                   VALUES ( COUNTRY,   '11', NOV )
  72.          WHEN ( DEC > 500 ) THEN
  73.                   INTO HIGH_AMOUNT(   REGION, MONTH, AMOUNT)
  74.                   VALUES ( COUNTRY,   '12', DEC )
  75. SELECT   COUNTRY,
  76.          JAN,
  77.          FEB,
  78.          MAR,
  79.          APR,
  80.          MAY,
  81.          JUN,
  82.          JUL,
  83.          AUG,
  84.          SEP,
  85.          OCT,
  86.          NOV,
  87.          DEC
  88. FROM     REGION_REVENUE
  89.  
Copyright exforsys.com



Read Next: Oracle Utilities - Extending the alert.log Analysis



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape