Solved

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

Posted on 2007-04-05
5
29,703 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
[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
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

623 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