Calling SQL scripts through Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling SQL scripts through Shell Script
# 15  
Old 06-08-2006
Thank You Thomas....

It is working fine....I need to run this and test this on real tables and see how it goes..

I really appreciate your time!
# 16  
Old 06-13-2006
Thomas....

All these days I was trying to do this and with your code I was able to execute successfully...

When the code review happened, they want to pass the table name as a parameter and told me to code it this way...I tried to put pieces together and couldn't go beyond this point..

I think this opens and closes connections. Is there any better way to do this?

Code:
#!/usr/bin/ksh
###################################################################################
SetupEnvironment () {
  ConnectionParameter="dataload/dataload@dadb"
}
###################################################################################
GetWorkingTableName() {
 sqlplus -silent /nolog @getAcxTableName.sql $ConnectionParameter 1.tmp >/dev/null
 status=$?
 if [[ $status -ne 0 ]]
 then
   print "Error from sql script getAcxTableName.sql"
   exit 1
 fi
 grep ACX 1.tmp | read tempval
 print "$tempval"
}
###################################################################################
Truncate_Table () {
  sqlplus $ConnectionParameter <<!
whenever sqlerror exit failure
set serveroutput on size 1000000
exec p_truncate("$1","$2");
exit;
!
  print "Error from sql"
}
###################################################################################
RunTheETLJob () {
 print "dummy"
}
###################################################################################
SwitchSynonym () {
 CREATE OR REPLACE PUBLIC SYNONYM ACXIOM_PROSPECT FOR $WorkTableName
 print "dummy"
}

################ M A I N   P R O G R A M ##########################################
SetupEnvironment

GetWorkingTableName|read WorkTableName
print "Tablename is $WorkTableName"
#exit 0
Truncate_Table $WorkTableName
export USER_VAR_0=$WorkTableName
print "Truncating table $WorkTableName"
RunTheETLJob
SwitchSynonym $WorkTableName
print "Switched synonym to $WorkTableName
exit 0;

I would really appreciate any help on this...
# 17  
Old 06-13-2006
Quote:
Originally Posted by madhunk
I think this opens and closes connections. Is there any better way to do this?
What do you mean that it opens and closes connections?
# 18  
Old 06-13-2006
Thomas,

I think I am opening and closing connections in every function. I am not sure of this too...I am not sure if this is a good way to code. I am learning everyday...

I could put all these pieces together after all these days..I want to pass the table name as a parameter during runtime which was received from the getAcxTableName.sql

Code:
whenever sqlerror exit failure
set feed off
set echo off
set pages 0
set trimspool on
connect &1
spool &2
select decode(table_name,'ACXIOM_PROSPECT_B','ACXIOM_PROSPECT_A','ACXIOM_PROSPECT_A','ACXIOM_PROSPECT_B','')
from all_synonyms where SYNONYM_NAME='ACXIOM_PROSPECT'
/
spool off;
exit;

I am really going crazy here!!
# 19  
Old 06-14-2006
Yes, you are opening and closing connections every time that you run sqlplus and provide it logon credentials. An alternative technique, which, since you are struggling so much, I do not recommend is to run sqlplus as a coprocessor, which keeps one session open no matter how many times that you need to interact with Oracle. This has advantages and disadvantages and, for me, the advantanges are substantial enough that I have developed libraries of functions to seemlessly and simply interact with up to four (which is the limit for ksh88) databases via coprocessors. However it is harder to do so you will be better served using the method that you been using. You are not doing that much work and opening and closing sessions isn't costing you that much for this load operation. A coprocessor implementation, while it would be a great learning experience, is not for the beginner.

As far as question goes, I'm not sure I understand what you are asking me. Is your script working or not? If not, what's wrong? If you want to combine all of your sqlplus calls then you certainly can but it's going to require more advanced sqlplus techniques or to do everything in PL/SQL. At any rate, it the script works, and, if performance isn't an issue (which, judging by the script simply can't possibly be the case), and, if you have plenty of other work to do, leave it alone and move on. When you get better and scripting, you can make changes then.

By the way, one comment regarding your script. You are logging on two different ways, once with the logon credentials as sqlplus parameters and once inside the here document. I would stick to the second method since it keeps your database password from being viewed by other Unix users.
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. Shell Programming and Scripting

shell script hangs while calling sql file

I have a master shell script which calls some 40 shell scripts. All the shell scripts calls a sql file which executes some sql statements. I run these scripts in parallel such that it saves me time. When i executed them i saw some strange behavior. Firstly, I found that some scripts among the 40... (1 Reply)
Discussion started by: sushi
1 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. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

6. 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

7. 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

8. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: rajagavini
5 Replies

9. 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

10. 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
Login or Register to Ask a Question