在 Unix 提示符下但不在 shell 脚本中工作的 Oracle 上执行 SQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19724699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute SQL query on Oracle working in Unix prompt but not in shell script
提问by xcentaur
I am trying to connect to an Oracle db using a ksh script. When I run this directly from the prompt, it works. But when I put it inside a script (abc.sh) it fails. Below is what I've put in the script (edited to make it shorter):
我正在尝试使用 ksh 脚本连接到 Oracle 数据库。当我直接从提示符运行它时,它可以工作。但是当我把它放在一个脚本(abc.sh)中时它失败了。以下是我在脚本中的内容(经过编辑以使其更短):
Here abc is the username, while abc$123 is the password of the user which has access on database DBNAME.
这里 abc 是用户名,而 abc$123 是有权访问数据库 DBNAME 的用户的密码。
#!/usr/bin/ksh
sqlplus -s /nolog << EOF > output
connect abc/abc3@DBNAME;
set echo off
set heading off
select table_name from dba_tables;
exit;
EOF
This works if typed directly, but run as ./abc.sh, gives error -
如果直接输入,这有效,但作为 ./abc.sh 运行,会出现错误 -
ERROR ORA-01017: invalid username/password; logon denied
I'm sure I'm missing something simple, but can't figure this out. Thanks for your help.
我确定我错过了一些简单的东西,但无法弄清楚。谢谢你的帮助。
回答by Dale Myers
You cannot just write code like this and expect it to be passed to the relevant program. Your SQL lines are being executed by KSH instead of your SQL server. Instead you need to do something more along the lines of:
您不能只是编写这样的代码并期望将其传递给相关程序。您的 SQL 行由 KSH 而不是您的 SQL 服务器执行。相反,您需要按照以下方式做更多的事情:
echo "SQL CODE HERE" | sqlplus ....