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

时间:2017-01-04 07:50:30

标签: oracle bash shell unix sqlplus

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

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

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

路径代码:

#!/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输出?非常感谢!

3 个答案:

答案 0 :(得分:0)

您的查询的输出将在下面的路径

进行假脱机
{LogDirectory}/query.txt

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

答案 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

答案 2 :(得分:-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