Solved

Pass table value to SQL Loader Control File

Posted on 2014-03-18
3
1,442 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 500 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

740 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