繁体   English   中英

在bash脚本中循环sql查询

[英]loop sql query in a bash script

我需要使用bash循环oracle sqlplus查询。

我的情况就是这样。 我在文本文件中有一组名称,我需要使用sqlplus查询找出该名称的详细信息。

textfile.txt内容:

john
robert
samuel
chris

bash脚本

#!/bin/bash

while read line
do
/opt/oracle/bin/sqlplus -s user@db/password @query.sql $line
done < /tmp/textfile.txt

sql查询:query.sql

set verify off
set heading off
select customerid from customers where customername like '%&1%';
exit

问题是当我运行脚本时出现类似以下错误

SP2-0734:未知命令以“ robert ...”开头-其余行被忽略。

有人可以告诉我如何解决吗?

我一直这样做的方式如下:

#!/bin/bash

cat textfile.txt |while read Name
do
sqlplus -s userid/password@db_name > output.log <<EOF
set verify off 
set heading off 
select customerid from customers where customername like '%${Name}%'
/
exit
EOF

Bash将自动神奇地扩展每一行的$ {Name}并将其放入sql命令中,然后再发送至sqlplus

您是否已将set define on 是您的通配符&吗? 您可以检查glogin.sql来了解。

是的,建立n连接以传递n查询可能不是一个好的解决方案。 也许对您来说开发起来会更快,并且您会一次完成,但是如果没有,您应该考虑制定一个过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM