Solved

Pass table value to SQL Loader Control File

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

792 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