shell脚本将表名作为参数传递给sqlplus?

时间:2016-03-04 15:07:34

标签: shell unix sqlplus

我有一个名为table.txt的文件,用于存储表名。我希望sql更新查询从我的table.txt文件中逐个获取表名。我的代码如下:

while read LINE1; do
`sqlplus username/pwd@tname <<END |sed '/^$/d'
set head off;
set feedback off;
update &LINE1 set enterprise_id = '1234567890' where enterprise_id is NULL;
update &LINE1 set sim_inventory_id ='1234567890';
COMMIT;
exit;
END`
done < table.txt

它给出了一个错误sqlplus未找到。你能告诉我什么错吗?

1 个答案:

答案 0 :(得分:0)

这与传递表名无关。找不到&#34; sqlplus&#34;错误意味着它无法找到二进制可执行文件,因此它不会尝试连接或运行SQL命令。

您的shell脚本只能在导出时看到来自调用shell的环境变量。如果您修改了PATH以包含sqlplus二进制文件的位置,那么您可能没有将其导出;设置后添加export PATH

或者您可以将脚本设置为不依赖于shell环境。

export ORACLE_HOME=/path/to/oracle/installation
export PATH=${ORACLE_HOME}/bin:$PATH
export LD_LIBRARY_PATH=${ORACLE}/lib:${LD_LIBRARY_PATH}

while read LINE1; do
sqlplus username/pwd@tname <<END |sed '/^$/d'
set head off;
set feedback off;
update &LINE1 set enterprise_id = '1234567890' where enterprise_id is NULL;
update &LINE1 set sim_inventory_id ='1234567890';
COMMIT;
exit;
END
done < table.txt

顺便说一下,两次更新同一张表是不必要的;你可以这样做:

update &LINE1 set enterprise_id = nvl(enterprise_id, '1234567890'),
  sim_inventory_id ='1234567890';

从文件内容创建所有更新语句的列表并在单个SQL * Plus会话中运行它们也会更快,因此您不会重复创建和删除连接。但这超出了你所要求的范围。