Solved

UNIX shell script to call and execute a SQL script in Oracle.

Posted on 2007-04-05
5
28,017 Views
Last Modified: 2013-12-18
I have couple of SQL statements that does an INSERT, UPDATE, DELETE operations.
Now, I would like to create a shell script to automate the process in UNIX.
Can you please help me in constructing an UNIX shell script that automates the process by calling the file from a specified location on the UNIX and executing the SQL commands from the file in Oracle?
Currently, I am running the DML statements manually in TOAD.
I worked on UNIX environment before and did not recently.
The SQL statements can be in files (.txt, .sql)
Please help.
Thanks in advance.


0
Comment
Question by:RND2006
5 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 168 total points
ID: 18858729
Try something like this:

Calling script (assuming oraenv is in your path):

#!/usr/bin/ksh
export ORACLE_HOME=/u01/oracle/product/9.2
export ORACLE_SID=mydb
export ORAENV_ASK=NO
. oraenv

sqlplus -s @myscript.sql



Then myscript.sql:

userid/password
insert.....
delete.....
update.....
exit


Make sure that the permissions on the .sql file are read only to the user running the script so that not everyone can see your user and password.
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 166 total points
ID: 18863944
my_shell_script:

sqlplus << EOF
userid/password@database
@unix_path/insert_script.sql
@unix_path/update_script.sql
@unix_path/delete_script.sql
exit
EOF

Then execute using -
./my_shell_script

Note - you may have to use the chmod command to make the my_shell_script executable.
0
 
LVL 3

Assisted Solution

by:imaredia
imaredia earned 166 total points
ID: 18908991
Create a script file "call_ora_script.sh"

Body of "call_ora_script.sh"
----------------------------------
#!/bin/sh
PATH=/usr/bin:/usr/local/bin
ORACLE_SID=dbname  #something like this oradb
ORACLE_HOME=/dba00/app/oracle/product/10.1
ORAENV_ASK=NO
. /usr/local/bin/oraenv  # path where oraenv resides
ORAENV_ASK=YES
ORACLE_BIN=$ORACLE_HOME/bin

sqlplus -s /NOLOG << EOF > log.txt
CONNECT username/passwd@dbname
whenever sqlerror exit sqlcode
set serveroutput on size 1000000
set feedback off
set line 200
declare
begin
   dbms_output.put_line('Hello');
end;
/
@/path/sqlfile_name.sql
@/path/txtfile_name.txt
EXIT
EOF

#To check if the script run fine
x=$?
if [ $x != 0 ] ; then
  echo "error..."
  exit 1
fi

Now execute the script "call_ora_script.sh"

If you want to hide the password, define it in the .profile and use the enviornemt variable in your script.
something like this DB_PASSWD=abc and call it as $DB_PASSWD in your script.
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.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
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…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

863 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

23 Experts available now in Live!

Get 1:1 Help Now