将值从sql脚本返回到shell脚本 [英] Return value from sql script to shell script

查看:289
本文介绍了将值从sql脚本返回到shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有调用以下sql脚本的shell脚本:

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'
                     );

现在,如何将这个PERCENTAGE_NUMBER变量返回到我的shell脚本中?

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

我的shell脚本如下:

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"

我希望将最后一个SELECT语句结果存储到shell脚本中的model_count变量中.

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?

推荐答案

我实际上所做的是我将这两个查询分开,并在我的shell脚本中分别调用了它们:

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

这篇关于将值从sql脚本返回到shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆