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

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.


RND2006Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve WalesSenior Database AdministratorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
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
imarediaCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.