Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-05
5
Medium Priority
?
30,271 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 23

Accepted Solution

by:
Steve Wales earned 672 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 664 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 664 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

721 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