Solved

Pass table value to SQL Loader Control File

Posted on 2014-03-18
3
1,314 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

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…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
This video shows how to recover a database from a user managed backup

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now