Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

oracle - Return value from sql script to shell script

I have shell script that calls the following sql script:

     INSERT INTO SEMANTIC.COUNT_STATISTICS (...);
     UPDATE SEMANTIC.COUNT_STATISTICS 
     SET PRNCT_CHANGE = 1.1;


  --want to store result of this bellow select statement in model_count variable

      select PRNCT_CHANGE
      FROM SEMANTIC.COUNT_STATISTICS
      WHERE model = '&MY_MODEL'
      AND NEW_DATE = (
                      select max(NEW_DATE)
                      from SEMANTIC.COUNT_STATISTICS
                      where MODEL = '&MY_MODEL'
                     );

Now, how do I return this PERCENTAGE_NUMBER variable back to my shell script?

My shell script is as follows:

#!/bin/bash
#
# setup oracle, java, and d2rq environment
. /etc/profile.d/oracle.sh
. /etc/profile.d/java.sh
. /etc/profile.d/d2rq.sh

cd /opt/D2RQ

model_count=$(sqlplus user/pass @count.sql 'MODEL')

if ["$model_count" > 0]; then
   echo "percentage count is positive"
else
   echo "its negative"

I would like for that last SELECT statement result to be stored into my model_count variable in shell script.

Anyone knows why is not working?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

A bash example with the use of a bash-function (note! database OS-authentication "/")

#!/bin/bash

get_count () {
    sqlplus -s / <<!
    set heading off
    set feedback off
    set pages 0
    select count(*) from all_objects where object_type = '$1'; 
!
}

count=$(get_count $1)

echo $count

if [ "$count" -gt 0 ]; then
    echo "is greater than zero"
else
    echo "is less or equal to zero"
fi


~/tmp/ $ ./count.sh INDEX
2922
is greater than zero
~/tmp/ $ ./count.sh TABLE
1911
is greater than zero
~/tmp/ $ ./count.sh FUNCTION
226
is greater than zero
~/tmp/ $ ./count.sh "SUPEROBJECT"
0
is less or equal to zero

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...