Solved

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

Posted on 2007-04-05
5
27,799 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 31

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Last record chosen in Oracle Query 3 35
unix in java example 9 43
Linux "time" command output redirection 16 77
sql query 9 22
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…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to recover a database from a user managed backup

743 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

13 Experts available now in Live!

Get 1:1 Help Now