【问题标题】:$ ignored when trying to execute query尝试执行查询时忽略 $
【发布时间】:2020-05-25 22:43:45
【问题描述】:

我使用以下 Oracle 命令找出 Oracle 数据库的日志记录模式:

select name,log_mode from v$database

该命令在交互式会话中运行没有任何问题:

SQL> select name,log_mode from v$database;

NAME      LOG_MODE
--------- ------------
T2       NOARCHIVELOG

但从脚本运行时无法正常运行:

#!/bin/sh
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1;export ORACLE_SID=T2;/oracle/app/oracle/product/12.1.0/dbhome_1/bin/sqlplus -L -s "user/password" <<EOF
select name,log_mode from v$database;
quit
EOF 

带有以下错误消息:

ERROR at line 1:
ORA-00942: table or view does not exist

检查stdout 时,我可以看到$ 符号之后的其余命令被忽略:

StdOut = select name,log_mode from v
                          *
ERROR at line 1:
ORA-00942: table or view does not exist

我尝试了多种方法来处理有问题的$ 符号,使用:""''"\,但到目前为止它们都没有帮助。我最近的尝试如下:

#!/bin/sh
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1;export ORACLE_SID=T2;/oracle/app/oracle/product/12.1.0/dbhome_1/bin/sqlplus -L -s "user/password" <<EOF
select name,log_mode from v'$'database;
quit
EOF

但现在标准输出带有 ' 单引号:

    StdOut = select name,log_mode from v'$'database
                           *
ERROR at line 1:
ORA-00933: SQL command not properly ended

值得一提的是,我使用的帐户同时具有SELECT ANY DICTIONARY 权限和SELECT_CATALOG_ROLE 角色。

【问题讨论】:

    标签: oracle unix sqlplus


    【解决方案1】:

    $ 在 Oracle 发现之前已被您的 shell 解释; she shell 正在替换未定义的$database。这与 Oracle 无关 - 它只是 shell 的工作方式。

    您已经意识到,您需要转义 $ 符号;你说你试过\$,但这就是你需要的:

    select name,log_mode from v\$database;
    

    所以:

    #!/bin/sh
    export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1;export ORACLE_SID=T2;/oracle/app/oracle/product/12.1.0/dbhome_1/bin/sqlplus -L -s "user/password" <<EOF
    select name,log_mode from v\$database;
    quit
    EOF
    

    哪个有效。

    【讨论】:

    • 正如我所写,我尝试使用 \$。并且仍然得到同样的错误。 ReturnCode = 0 StdOut = select name,log_mode from v * ERROR at line 1: ORA-00942: table or view does not exist StdErr = /usr/bin/ksh: ^M: not found
    • 你用什么来运行这个 - 它看起来不像你只是在命令行上执行你的 shell 脚本,来自ReturnCode 等标签;并且不确定ksh 参考来自哪里。如果你有另一个级别的 shell 做某事,也许两次转义 (v\\$database) 可能会有所帮助,但如果没有看到中间层在做什么,就很难说清楚。请将它与您的操作系统以及所涉及的shksh 版本一起添加到问题中。
    【解决方案2】:

    尝试使用&lt;&lt;'EOF' 而不是&lt;&lt;EOF 以避免在输入数据(“here-document”)中替换外壳,请参阅
    https://mywiki.wooledge.org/HereDocument
    https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_07_04

    #!/bin/sh
    export ORACLE_HOME=/oracle/app/oracle/product/12.1.0/dbhome_1
    export ORACLE_SID=T2
    /oracle/app/oracle/product/12.1.0/dbhome_1/bin/sqlplus -L -s "user/password" <<'EOF'
    select name,log_mode from v$database;
    quit
    EOF 
    

    【讨论】:

      猜你喜欢
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-06
      • 2018-07-24
      • 1970-01-01
      • 2016-04-25
      相关资源
      最近更新 更多