Solved

How to run sql or pl/sql block using shell script?

Posted on 2011-09-20
5
574 Views
Last Modified: 2012-05-12
I am running simple sql query using shell script and store the result in a variable. I ran the below code but result is not storing in the output. Please advise.

# Oracle Environment Variables
############################################################################
. $HOME/.profile

ORA_USER=user
ORA_PWD=password
############################################################################
# Local Variables
############################################################################
run_date=`date '+%Y%m%d'`
run_time=`date '+%H%M%S'`

shell_dir=$HOME/analytics/bin
sql_dir=$HOME/analytics/sql
tmp_dir=$HOME/analytics/tmp
log_dir=$HOME/analytics/log

progName=${0##*/}
sql_file=${sql_dir}/${progName%%.*}.sql
tmp_file1=${tmp_dir}/${progName%%.*}_${run_date}${run_time}.1.sql
tmp_file2=${tmp_dir}/${progName%%.*}_${run_date}${run_time}.2.sql
log_file=${log_dir}/${progName%%.*}_${run_date}${run_time}.log

start_time=`date +%Y-%m-%d-%H:%M:%S`

echo "Sample script is started at $start_time." >> ${log_file}

cat <<- EOF > $tmp_file1
set echo off;
set term off;
set heading off;
set feedback off;
whenever sqlerror exit;
select sysdate
from dual;
exit;
EOF

seg_snap_date=$(sqlplus ${ORA_USER}/${ORA_PWD}@DATABASE @$tmp_file1) >> ${log_file}

echo "Today's date $seg_snap_date" >> ${log_file}

cat $log_file

exit $?
0
Comment
Question by:IT_ETL
  • 2
  • 2
5 Comments
 
LVL 15

Assisted Solution

by:Devinder Singh Virdi
Devinder Singh Virdi earned 200 total points
ID: 36570253
Use Method 1

var= `sqlplus -s username/pass@db @file_to_execute.sql`

Method 2:

sqlplus -L -silent username/pass@db |&

print -p -- 'set feed off pause off pages 0 head off veri off line 500'
print -p -- 'set term off time off'
print -p -- " set sqlprompt '' "
print -p -- "select sysdate from dua;"

read -p ORACLE_DT
# YOU CAN USE UNIX COMMAND HERE

print -p exit ;

0
 

Author Comment

by:IT_ETL
ID: 36571777
I have modified above code as you suggested below. But still result from sql block is not stored in the variable.

cat <<- EOF > $tmp_file1
set echo off
set term off
set heading off
set feedback off
whenever sqlerror exit
select sysdate
from dual;
exit;
EOF

seg_snap_date=`sqlplus -s ${ORA_USER}/${ORA_PWD}@ADWPRD @$tmp_file1`
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 300 total points
ID: 36573464
Try this:
...
set term on
...
0
 

Author Comment

by:IT_ETL
ID: 36574735
Thanks slightwv! It worked.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36574763
Glad to help.  did you go back and try your original code?  My shell scripting is rusty but it should work as well.
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

Title # Comments Views Activity
Call Shell Script from Perl Script 6 79
compre toata in where clue oracle 4 57
Migrate database to ASM disks. 1 40
Fastest way to replace data in Oracle 5 51
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Utilizing an array to gracefully append to a list of EmailAddresses
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

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