Calling SQL LDR and SQL plus scripts in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling SQL LDR and SQL plus scripts in a shell script
# 1  
Old 11-05-2007
Calling SQL LDR and SQL plus scripts in a shell script

Hi-
I am trying to achieve the following in a script so I can schedule it on a cron job. I am fairly new to the unix environment...

I have written a shell script that reads a flat file and loads the data into an Oracle table (Table1) via SQLLDR. This Works fine. Then, I run a nested insert statement to parse through the records in Table1 and load the necessary results into a Table. (Table2). This works fine too.

Here is how my script looks like...

# Always truncate table 1 before loading data.
print "truncate table table1;" | sqlplus -s userid/password@db>$now.log
sqlldr userid/password@db control=dbload.ctl>>$now.log
# Always truncate table 2 before loading data.
print "truncate table table2;" | sqlplus -s userid/password@db>>$now.log
# Parse through Table1 and load only necessary records to Table2.
print "insert into table2 select id, min(STARTDATE) as startdate from table1 group by id, to_char(STARTDATE,'mm/dd/yyyy');" | sqlplus -s userid/password@db>>$now.log

I call this script myscript.sh, which will be run via crontab every 15 mins.

Here are my questions:
1) Note how I establish a database connection to execute every sql I need to run? I am thinking there should be an easy way to this. I looked into the "here documents" but not sure how I can use them above. Do I really need to use the "here document" or is my script good the way as it is now?
Would it open and close the db connections for each sql correctly?

If I do use the "here document", do I need to have one for the sqlldr and the other for all the sqlplus part?

Please advise best way to go ahead.
Thanks
RG
# 2  
Old 11-05-2007
Using the "here document"

Using a "here" document would essentially be the same as using a long string. These are equivalent:

Code:
print "query1
query2
query3" | sqlplus

Code:
cat <<-\EOF | sqlplus
         query1
         query2
         query3
EOF

Code:
print "query1" | sqlplus
print "query2" | sqlplus
print "query3" | sqlplus

Either of the first two should be more efficient than the last. Take this example:


Code:
# This takes a long time (20-30 sec)
i=0
time while (((i+=1)<100)); do
    rsh localhost date;
done

# This is fast!  1 second!
cat <<-\EOF | rsh localhost sh
                i=0
                while (((i+=1)<100)); do
                        date
                done
                EOF

Since I use "-\EOF", the dash "-" removes any leading tabs from each line. The backslash "\" prevents all the text from being evaluated, and without it I would need to escape any special characters.
# 3  
Old 11-05-2007
Reply to gus2000

Thank you. It helps to know that options 1 and 2 are better than 3, which is what I am using now. So, let us say I will go and update my script in line with option 1.

Here are my concerns though...

The following is the order in which my sql commands must run...

1)truncate table1 ==> This is sqlplus command.
2) Load data into table1 ==> This is a sqlldr command.
3) truncate table2 ==> This is sqlplus command.
4) Insert into table2 from table1 ==> This is sqlplus command.

So, #2 is a sqlldr command and not simply a query I can put in sqlplus along with #1, #3 and #4.

How would you recommend that I use options 1 or 2 to achieve the above?
Would I not have to open sqlplus for #1, followed by sqlldr for #2 and then sqlplus again for #3 and #4 combined?

Also, is it okay to have the username and password in the script or should I store it somewhere else? The way I am using it now, can some one use the ps grep command to view the password?

Thank you.
RG
# 4  
Old 11-05-2007
Possible solution?

Here is what I did...

In the "myscript.sh" script which is my main script, I do the following call...
sqlplus -s @load.sql>$now.log

load.sql is my file that contains all the queries and the sqlldr command
This is what is inside load.sql

username/password@database
truncate table table1;
! sqlldr username/password@database control=dbload.ctl
truncate table table2;
insert into table2 select id, min(STARTDATE) as startdate from table1 group by id, to_char(STARTDATE,'mm/dd/yyyy');
exit

I found out that using the "!" option with in sqlplus gives the control to the OS to run my sqlldr command, complete the dataload and then continue with the remaining 2 sql commands.

Please let me know if I got it all correctly. Should I be looking for anything else?

Thanks
RG
# 5  
Old 11-05-2007
You can add error control (before first truncate).
For example :
Code:
whenever sqlerror exit failure;
whenever  oserror exit failure;

Jean-Pierre.
# 6  
Old 11-05-2007
error control

So here is how my load.sql will look like then?
sorry, I am not much of a sqlplus user either, still learning...
Let us say the below is correct, what would this error control help me achieve?
In other words, what would I achieve by testing for a sqlerror even before I execute my first sql statement? Would my first sql not fail if there was a sql error to begin with?

Please advise.
Thanks
RG

username/password@database
whenever sqlerror exit failure;
whenever oserror exit failure;

truncate table table1;
! sqlldr username/password@database control=dbload.ctl
truncate table table2;
insert into table2 select id, min(STARTDATE) as startdate from table1 group by id, to_char(STARTDATE,'mm/dd/yyyy');
exit
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Calling PL/SQL Block into Shell Script

Hi, i have one simple PL/SQL Block and i have saved it as .sql file, which i am trying to call from UNIX script. PL/SQL block structure CONNECT DB_NAME/PWD@Database whenever SQLERROR EXIT 1; Declare ..Variables... BEGIN --Code-- exception END; exit; I have save this block as... (3 Replies)
Discussion started by: abhii
3 Replies

2. UNIX for Advanced & Expert Users

Call parallel sql scripts from shell and return status when both sql are done

Hi Experts: I have a shell script that's kicked off by cron. Inside this shell script, I need to kick off two or more oracle sql scripts to process different groups of tables. And when both sql scripts are done, I will continue in the shell script to do other things like checking processing... (3 Replies)
Discussion started by: huasheng8
3 Replies

3. Shell Programming and Scripting

Calling sql file from shell script with parameters.

Hi, I am calling a sql file script.sql from shell script and passing few parameters also as shown below: sqlplus -S id/password @script.sql $param1 $param2 Now,In sql file I have to create a extract text file after querying oracle tables based on the parameters passed(param1,param2) as... (7 Replies)
Discussion started by: anil029
7 Replies

4. Shell Programming and Scripting

calling a sql file in my shell script

Hi, I want to call a sql file in my shell script. see the below code:- if ] then ( isql -U${S_USER} -S${S_SERV} -w100 -b -h0 <<ENDSQL | sed -e "s/Password://" ${S_PWD} set nocount on go use ${S_DB} go // need to call a file name... (16 Replies)
Discussion started by: dazdseg
16 Replies

5. UNIX for Advanced & Expert Users

Calling sql file from shell script

Hi I have a shell script that call a sql file. The sql file will create a spool file. My requirement is, when ever i get an OS error like file not found. I have to log it in a log file. Could some who worked in a like scenario help me by giving the code sample. Many Thanks.. (1 Reply)
Discussion started by: chintapalli001
1 Replies

6. Shell Programming and Scripting

Calling sql in shell script with parameters

Dear All, I want to call an sql script within a unix shell script. I want to pass a parameter into the shell script which should be used as a parameter in teh sql script. e.g $ ./shell1.sh 5000129 here 5000129 is a prameter inside shell script i am calling one sql script e.g. ... (2 Replies)
Discussion started by: Radhe
2 Replies

7. Shell Programming and Scripting

any possible solution on sql calling scripts

hi all, i have a function which will take i/p as a ddl sctipt as i/p and execute it, let function execute_sql { db_var="$1" v_cnt=`sqlplus -s XXXXX/XXXXX@aXXX << ENDSQL | sed -e "s/Connected\.//" -e "/^$/d" set pagesize 0 feedback off verify off heading off echo off serveroutput on size... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

8. Shell Programming and Scripting

calling sql file from shell script

Hello everybody I need help calling sql file from shell script. Can anyone help me creating a small shell script which calls an sql file . The .sql file should contain some select statements like select emp_no from emp_table; select emp_id from emp_table; And the results should be... (6 Replies)
Discussion started by: dummy_needhelp
6 Replies

9. UNIX for Advanced & Expert Users

Calling PL/SQL Script in Shell Programming

Hi all, In a shell script I need to pass two parameters to a pl/sql script and get the ouput of the pl/sql script and use it in shell script. For example Shell script : test.sh PL/SQL script : get_id.sql parameter1 parameter2 Actually get_id.sql has a select statement something... (1 Reply)
Discussion started by: lijju.mathew
1 Replies

10. Shell Programming and Scripting

Calling SQL scripts through Shell Script

Oracle and Scripting gurus, I need some help with this script... I am trying to add the query SELECT * FROM ALL_SYNONYMS WHERE SYNONYM_NAME = 'METADATA' in the current script.... Read the result set and look for the TABLE_NAME field. If the field is pointing to one table eg.... (18 Replies)
Discussion started by: madhunk
18 Replies
Login or Register to Ask a Question