Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pass table value to SQL Loader Control File

Posted on 2014-03-18
3
Medium Priority
?
1,720 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

722 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