如何在 shell 脚本中连接数据库并运行查询?

问题描述 投票:0回答:3

我正在尝试从 shell 脚本连接到数据库,但出现以下错误。

数据库输出:错误: ORA-12154: TNS: 无法解析指定的连接标识符

SP2-0306:选项无效。 用法:CONN[ECT] [登录] [AS {SYSDBA|SYSOPER}] 其中 ::= [/][@] | / SP2-0306:选项无效。 用法:CONN[ECT] [登录] [AS {SYSDBA|SYSOPER}] 其中 ::= [/][@] | /

路线代码:

#!/bin/bash

LogDirectory='/users/users-06/p6***8/scripts/dir'

ORACLE_HOME=/tools/ver/oracle-10.2.0.1-64
export ORACLE_HOME

DBUSER='p6*02*1'
DBUSERPASSWORD='R****07'
DB='O**XDA3'

var=`$ORACLE_HOME/bin/sqlplus -S ${DBUSER}/${DBUSERPASSWORD}@${DB} << EOD
spool ${LogDirectory}/query.txt
set linesize 32767
set feedback off
set heading off
SELECT * FROM Omi.ESP_FEED_REQUEST WHERE FEED_NAME='PSAR_TRANSACTION_FEED' AND REQUEST_ID='3694707322503' AND AS_OF='04-Jan-2017' ORDER BY 1 DESC;
spool off
exit;
EOD`

echo $var > ${LogDirectory}/DB_output.txt

您能否建议我如何在“var”变量中获取 sql 输出?非常感谢!

oracle bash shell unix sqlplus
3个回答
0
投票

您的查询的输出正在以下路径中进行假脱机

{LogDirectory}/query.txt

var
只会有代码 1 或 0 的状态。如果该语句成功执行,则 var status 将为
0
else
1


0
投票

这里工作正常:

#!/bin/bash

LogDirectory='/home/oracle'

ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export ORACLE_HOME

DBUSER='scott'
DBUSERPASSWORD='scott'
DB='db11g'

var=`$ORACLE_HOME/bin/sqlplus -S ${DBUSER}/${DBUSERPASSWORD}@${DB} << EOD
spool ${LogDirectory}/query.txt
set linesize 32767
set feedback off
set heading off
select 5 from dual;
exit;
EOD`

echo "Database output: ${var}"

$ ./stack.sh
Database output:
         5

您的数据库是否已启动并正在运行?检查:

ps -ef | grep pmon

这将显示一个正在运行的进程。如果不是,您会收到以下错误:

SQL> shu immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
$ ./stack.sh
Database output: ERROR:
ORA-12514: TNS:listener does not currently know of service requested in connect
descriptor


SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

编辑XING:

我将代码更改为:

 select * from t1;

$ ./stack.sh
Database output:
A
B

并从 user_tables 中选择 *

$ ./stack.sh

Database output:
DEPT                           USERS                                                                                  VALID             10                     1        255          65536     1048576           1  2147483645           YES N           4          5            0          0          0          20                         0             0           1          1     N ENABLED            4 07-OCT-14 NO               N N NO  DEFAULT DEFAULT DEFAULT DISABLED YES NO             DISABLED YES                                DISABLED DISABLED              NO  NO  YES DEFAULT
SALGRADE                       USERS                                                                                  VALID             10                     1        255          65536     1048576           1  2147483645           YES N           5          5            0          0          0          10                         0             0           1          1     N ENABLED            5 07-OCT-14 NO               N N NO  DEFAULT DEFAULT DEFAULT DISABLED YES NO             DISABLED YES                                DISABLED DISABLED              NO  NO  YES DE

-1
投票
echo "SELECT * FROM Omi.ESP_FEED_REQUEST WHERE FEED_NAME='PSAR_TRANSACTION_FEED' AND REQUEST_ID='3694707322503' AND AS_OF='04-Jan-2017' ORDER BY 1 DESC;" | sqlplus -s $DBUSER@$DB/$DBUSERPASSWORD >> dboutput.txt
© www.soinside.com 2019 - 2024. All rights reserved.