SQL查询在运行shell脚本时返回0,但检查DB时6

发布于 2025-02-12 14:50:38 字数 1266 浏览 0 评论 0 原文

我正在尝试运行一个带有SQL查询的Shell脚本。现在,由于故事要求,我无法在Shell脚本中使用SQL脚本。我一直在尝试获取shell脚本以返回正确的计数,即“ 6”,但它只是返回“ 0”。

#!/bin/ksh
. /apps/path/config/setenv.ksh
DATE=`date "+%m%d%Y`
returnMessage="`sqlplus  username/password@$ORACLE_SID << EOF
WHENEVER OSERROR EXIT SQL.OSCODE ;
WHENEVER SQLERROR EXIT SQL.OSCODE ;

spool /apps/path/data/test.txt
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET ECHO ON
SET PAGES 0
SET LINESIZE 90

select count(*) from table where dt = to_date('06/18/2020','MM/DD/YYYY');
EOF
`
"
        exitCode=$?
        oracleError=`echo "$returnMessage" | grep ORA-`
        if [ -n "$oracleError" -o "$exitCode" -ne 0 ]; then
                log "An error occurred while looking up the $COUNT"
                log "SQLPlus Exit Code = $exitCode"
                log "SQLPlus Message is: $returnMessage"
                return 1
        fi
        export COUNT=`echo $returnMessage"
        return 0

输出也下方给出了

SQL> SET HEADING OFF
SQL> SET FEEDBACK OFF
SQL> SET VERIFY OFF
SQL> SET ECHO ON
SQL> SET PAGES 0
SQL> SET LINESIZE 90
SQL>
SQL> select count(*) from table where dt = to_date('06/18/2020','MM/DD/YYYY');
         0
SQL>

我正在使用的输出和代码。不确定问题在哪里,因为查询应返回6;

I'm trying to run a shell script that has a SQL query in it. Now, I can't use a SQL script in the shell script because of story requirements. I have been trying to get the shell script to return the correct count which is '6' but it is only returning '0'.

#!/bin/ksh
. /apps/path/config/setenv.ksh
DATE=`date "+%m%d%Y`
returnMessage="`sqlplus  username/password@$ORACLE_SID << EOF
WHENEVER OSERROR EXIT SQL.OSCODE ;
WHENEVER SQLERROR EXIT SQL.OSCODE ;

spool /apps/path/data/test.txt
SET HEADING OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET ECHO ON
SET PAGES 0
SET LINESIZE 90

select count(*) from table where dt = to_date('06/18/2020','MM/DD/YYYY');
EOF
`
"
        exitCode=$?
        oracleError=`echo "$returnMessage" | grep ORA-`
        if [ -n "$oracleError" -o "$exitCode" -ne 0 ]; then
                log "An error occurred while looking up the $COUNT"
                log "SQLPlus Exit Code = $exitCode"
                log "SQLPlus Message is: $returnMessage"
                return 1
        fi
        export COUNT=`echo $returnMessage"
        return 0

The output is also given below

SQL> SET HEADING OFF
SQL> SET FEEDBACK OFF
SQL> SET VERIFY OFF
SQL> SET ECHO ON
SQL> SET PAGES 0
SQL> SET LINESIZE 90
SQL>
SQL> select count(*) from table where dt = to_date('06/18/2020','MM/DD/YYYY');
         0
SQL>

THis is the output and the code I'm using. Not sure where it is going wrong since the query should return 6;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2025-02-19 14:50:38

un 提交 TED数据仅在创建它的会话中可见(并且,如果没有 CONDE> CONDE> CONDE> CONDE> CODER 在会话结束时将可见。泰德)。如果您看不到另一个会话中的数据(即SQL*ins shell中调用),请确保您已在SQL客户端中发布 commit 命令,其中 insert 数据。

注意:即使您将作为同一用户连接,这也将创建一个单独的会话,并且您将无法在另一个会话中看到未承诺的数据。

如果您已发出 commits and仍然看不到数据,然后确保SQL客户端和Shell程序都连接到同一服务器和同一数据库,并且正在查询该数据库的同一用户模式。

UnCOMMITted data is only visible within the session that created it (and will ROLLBACK at the end of the session if it has not been COMMITted). If you can't see the data from another session (i.e. in SQL*Plus invoked from the shell) then make sure you have issued a COMMIT command in the SQL client where you INSERTed the data.

Note: even if you connect as the same user, this will create a separate session and you will not be able to see the uncommitted data in the other session.

If you have issued a COMMIT and still can't see the data then make sure that both the SQL Client and the shell program are connecting to the same server and the same database and are querying the same user's schema of that database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文