Return value from sql script to shell script

走远了吗. 提交于 2019-11-27 23:13:34

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

What I actually did is I separated those 2 queires and called them separatelly in my shell script:

sqlplus -S user/pass << EOF
whenever sqlerror exit 1;
set echo on
@/opt/D2RQ/model_count.sql '$MODEL'  <--model_count.sql still has those INSERT & UPDATE statements
exit;
EOF

model_count=`sqlplus -S user/pass << EOF
SELECT PRNCT_CHANGE
 FROM COUNT_STATISTICS
WHERE model = '$MODEL'
AND NEW_DATE = (
                select max(NEW_DATE)
                from COUNT_STATISTICS
                where MODEL = '$MODEL'
               );
exit;
EOF`


if [ $model_count >= 0 ]; then
    echo "$model_count"
else
         echo "'$MODEL' is negative " | mail -s "scripts issues" -c angelina1984@aol.com
fi
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!