oracle – 如何验证sqlplus可以连接?

前端之家收集整理的这篇文章主要介绍了oracle – 如何验证sqlplus可以连接?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道是否有可能以某种方式获取sqlplus输出以发现我的数据库是否已启动.

我想在数据库上运行一个脚本列表,但在此之前,我想知道数据库是否已启动并运行我的脚本.

这是我尝试过的:

  1. sqlplus /@DB1 << EOF
  2. > select 1 from dual;
  3. > EOF

它无法连接,但sqlplus的返回码仍然说“一切正常”!

  1. sql*Plus: Release 11.2.0.4.0 Production on Mon Nov 28 10:06:41 2016
  2.  
  3. Copyright (c) 1982,2013,Oracle. All rights reserved.
  4.  
  5. ERROR:
  6. ORA-12505: TNS:listener does not currently know of SID given in connect
  7. descriptor
  8.  
  9.  
  10. Enter user-name: SP2-0306: Invalid option.
  11. Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
  12. where ::= [/][@]
  13. ::= [][/][@]
  14. Enter user-name: ju@srv:/tmp/jcho $echo $?
  15. 0

我知道我可以grep我的测试查询的结果,像这样:

  1. sqlplus /@DB1 << EOF
  2. select 'ALL_GOOD_BOY' from dual;
  3. EOF

呼叫:

如果连接有效,则给出1行,否则为0:

  1. $a.sh |grep ALL_GOOD_BOY|wc -l

……这对我来说似乎有很多步骤.在“无法连接”的模式下设置sqlplus的任何其他方式都会给出“错误”返回码?

感谢@Kacper提供的参考,我可以将这个sqlplus / nolog改编为我的案例;这是个主意:

>仅在没有连接的情况下打开sqlplus
>在sqlERROR上设置特定的返回码 – 这是连接失败时发生的情况
>可以像往常一样在调用者脚本中收集返回代码

  1. sqlplus /nolog << EOF
  2. WHENEVER sqlERROR EXIT 50
  3. WHENEVER OSERROR EXIT 66
  4. connect /@${MISTERY_DB}
  5. exit;
  6. EOF

然后电话:

  1. /ju $export MISTERY_DB="eg_NON_EXISTING_DB"
  2. /ju $a.sh
  3. sql*Plus: Release 11.2.0.4.0 Production on Tue Nov 29 08:43:44 2016
  4. Copyright (c) 1982,Oracle. All rights reserved.
  5. sql> sql> sql> ERROR:
  6. ORA-12154: TNS:could not resolve the connect identifier specified
  7. /ju $echo $?
  8. 50

还有关:Connect to sqlplus in a shell script and run SQL scripts

猜你在找的Oracle相关文章