有没有办法从 shell 脚本中保留 sqlplus 连接

Is there an way to preserve the sqlplus connection from shell script

在 DB2 中,在连接到 DB 之后,除非我们专门使用 TERMINATE 命令,否则连接会保持打开状态,并且可以用于在 shell 脚本中使用相同连接执行多个 sql . 有什么方法可以在来自 shell 脚本的 oracle sqlplus 中做同样的事情吗?

例如: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

感谢您的帮助。我找到了另一种解决方案,我认为它最适合我的要求并且工作得很好。 通过使用 Korn Shell 协进程访问 SQL*Plus。 下面是我的示例 运行,它完美地给出了所有结果。

#!/bin/ksh
##

##

output=""                  
set -f output              

integer rc                
typeset -r ERRFILE=[=10=].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]}"