?
Solved

Pass table value to SQL Loader Control File

Posted on 2014-03-18
3
Medium Priority
?
1,928 Views
Last Modified: 2014-03-22
There is a process that loads and verifies files in Oracle on Unix (AIX).

For example, a file called ORANGES.txt gets validated and gets assigned a file_key = 459 in the DATA_STG table.

Now its time to load this file, ORANGES.txt using SQL Loader.

Here's the control file:
LOAD DATA
APPEND INTO TABLE DATA_PROD
FIELDS TERMINATED by '|' TRAILING NULLCOLS
(
FILE_FILE_KEY      CONSTANT $FILE_KEY,
INSERTTIME      "TO_TIMESTAMP(SYSDATE)",
USER_ID                      CONSTANT "AA",
DATA_PROD_SK      "SEQ.NEXTVAL")

FILE_FILE_KEY needs to come from the table DATA_STG, which means it needs to be 459.

I know that I need a shell script to get the FILE_KEY from the table DATA_STG. I need help writing that piece. I've done some research and know that I can dynamically create the control file with this value, I'm not sure how. Any help would be appreciated.
0
Comment
Question by:SharonBernal
  • 2
3 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39938785
Perhaps this will work:
LOAD DATA
APPEND INTO TABLE DATA_PROD
FIELDS TERMINATED by '|' TRAILING NULLCOLS
(
FILE_FILE_KEY      "SELECT FILE_KEY FROM DATA_STG WHERE FILE_NAME='ORANGES.txt'"
INSERTTIME      "TO_TIMESTAMP(SYSDATE)",
USER_ID                      CONSTANT "AA",
DATA_PROD_SK      "SEQ.NEXTVAL"
)

Open in new window

0
 
LVL 35

Expert Comment

by:johnsone
ID: 39939280
I am going to assume that there is more than just the one file that goes through this process.

Since you already have the code to get the value of the constant, what I would do in this situation is write a temporary control file from the script and then pass that to SQL*Loader.

Something like this:

tmpctlfile="/tmp/tmpctlfile.$$"
echo 'LOAD DATA' > $tmpctlfile
echo 'APPEND INTO TABLE DATA_PROD' >> $tmpctlfile
echo 'FIELDS TERMINATED by '|' TRAILING NULLCOLS' >> $tmpctlfile
echo '(' >> $tmpctlfile
echo "FILE_FILE_KEY      CONSTANT $FILE_KEY," >> $tmpctlfile
echo 'INSERTTIME      "TO_TIMESTAMP(SYSDATE)",' >> $tmpctlfile
echo 'USER_ID                      CONSTANT "AA",' >> $tmpctlfile
echo 'DATA_PROD_SK      "SEQ.NEXTVAL")' >> $tmpctlfile

Open in new window


Then once the load is complete, delete the temporary control file.
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 2000 total points
ID: 39939572
OK, maybe this:
# After you get the "$FILE_KEY" ...
CTLFIL=/tmp/${FILE_KEY).ctl
cat - <<!EOF > $CTLFIL
LOAD DATA
APPEND INTO TABLE DATA_PROD
FIELDS TERMINATED by '|' TRAILING NULLCOLS
(
FILE_FILE_KEY      CONSTANT $FILE_KEY,
INSERTTIME         "SYSTIMESTAMP",
USER_ID            CONSTANT "AA",
DATA_PROD_SK       "SEQ.NEXTVAL"
)
!EOF
# . . .   E t c 

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question