有什么方法可以保存shell脚本中的sqlplus连接?

问题描述 投票:0回答:1

在DB2中,连接到DB后,除非我们特别使用TERMINATE命令,否则连接一直保持开放,并且可以在shell脚本中使用相同的连接执行多个sql。有什么方法可以在oracle sqlplus中用shell脚本做同样的事情吗?

例如:bash脚本可能会像下面一样

1. start of bash
2. sqlplus connection created
3. some bash commands
4. execute query using the same sqlplus connection created in 2nd step
5. some bash commands
6. execute query using the same sqlplus connection created in 2nd step
bash oracle shell sqlplus
1个回答
0
投票

谢谢你的帮助。我已经找到了另一个解决方案,我认为最适合我的要求,其工作完全正常.通过访问SQL*Plus使用Korn Shell Coprocess.下面是一个例子,我已经运行,它已经给出了所有的结果完美。

#!/bin/ksh
##

##

output=""                  
set -f output              

integer rc                
typeset -r ERRFILE=$0.err  
typeset -r EOF="DONE"      

## Create the error file or zero it out if it already exists.
> $ERRFILE

## Start sqlplus in a coprocess.
sqlplus -s connection |&

##  Exit SQL/Plus if any of the following signals are received:
##  0=normal exit, 2=interrupt, 3=quit, 9=kill, 15=termination
trap 'print -p "exit"' 0 2 3 9 15

## Send commands to SQL/Plus.
print -p "set heading off;"
print -p "set feedback off;"
print -p "set pagesize 0;"
print -p "set linesize 500;"

##
## Send a query to SQL/Plus.  It is formatted so we can set a shell variable.
##
print -p "select 'COUNT1='||count(*) as count from dual;"
print -p "prompt $EOF"  

while read -p output
do
   if [[ "$output" == "$EOF" ]]; then
     break
   else
     ## eval forces the shell to evaluate the line twice.  First, replacing
     ## "$output" with "COUNT1=99999", then again which creates and sets
     ## a variable.
     eval $output
   fi
done

##
##  Send another query to the same running sql/plus coprocess.
##
print -p "select 'COUNT1_DATE='|| sysdate as count_date from dual;"
print -p "prompt $EOF"
while read -p output
do
   if [[ "$output" == "$EOF" ]]; then
     break
   else
     eval $output
   fi
done

print -p "select 'COUNT2='||count(*)||';COUNT2_DATE='||sysdate from dual;"
print -p "prompt $EOF"
while read -p output
do
   if [[ "$output" == "$EOF" ]]; then
     break
   else
     eval $output
   fi
done


print -p "select count(*)||'|'||sysdate from dual;"
print -p "prompt $EOF"

while read -p output
do
   if [[ "$output" == "$EOF" ]]; then
     break
   else
     IFS="|"                      ## Set the Input Field Separator.
     set -A output_array $output  ## Create an array. It parses
                                  ## automatically on the IFS character.
   fi
done

print "COUNT1 count is $COUNT1"
print "COUNT1 date is  $COUNT1_DATE\n"
print "COUNT2 count is $COUNT2"
print "COUNT2 date is  $COUNT2_DATE\n"
print "Array count3:   ${output_array[0]}"
print "Array date3:    ${output_array[1]}"
© www.soinside.com 2019 - 2024. All rights reserved.