简体   繁体   English

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

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

In DB2, after connecting to DB, until and unless we are specifically use TERMINATE command, the connection keeps open and can be used for several sql execution using same connection inside a shell script.在 DB2 中,连接到 DB 后,除非我们专门使用 TERMINATE 命令,否则连接保持打开状态,可用于在 Z2591C98B70119FE624898B1E424B5E91 中使用相同连接执行多个 sql 执行。 Is there any way to do the same in oracle sqlplus from shell script?有没有办法在 shell 脚本的 oracle sqlplus 中做同样的事情?

For Example: bash script may look like below例如: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

thanks for the help.谢谢您的帮助。 I have found another solution which I think best suits my requirement and its working perfectly fine.我找到了另一种我认为最适合我的要求并且运行良好的解决方案。 By accessing SQL*Plus using a Korn Shell Coprocess.通过使用 Korn Shell 协进程访问 SQL*Plus。 Below is an example I have run and it has given all results perfectly.下面是我运行的一个示例,它完美地给出了所有结果。

#!/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]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM