各位,
我试图从shell脚本中调用带有1个参数的sql脚本。我的脚本如下
tab.sql
set lines 500
col file_name for a80
select file_name,bytes/1024/1024,maxbytes/1024/1024
from dba_data_files
where tablespace_name=upper('&TAB_NAME');
/我的shell脚本是test.sh
#!/bin/bash
LOC=`pwd`
echo -n "Enter the name of the Tablespace: "
read -r TAB_NAME
sqlplus "/ as sysdba" <<- EOF
@$LOC/tab.sql $TAB_NAME
EOF
exit当我执行脚本时,这就是我要得到的
sh test.sh
Enter the name of the Tablespace: users
SQL*Plus: Release 11.2.0.4.0 Production on Thu Nov 16 14:17:45 2017
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected.
SQL> Enter value for tab_name:
SP2-0546: User requested Interrupt or EOF detected.
Enter value for tab_name:
SP2-0546: User requested Interrupt or EOF detected.
SQL> Disconnected有人能告诉我这里出了什么问题吗?我在网上搜索了这个SP2-0546,但没有解决我的问题。我在OLE-6上使用11.2.0.4db
谢谢
发布于 2017-11-16 09:35:33
您混淆了命名和位置性替换变量,可能还会将它们与shell变量混淆一些。您可以在位置上引用在SQL*Plus命令行上传递的参数:
select file_name,bytes/1024/1024,maxbytes/1024/1024
from dba_data_files
where tablespace_name=upper('&1');
/如果您愿意,您可以定义一个命名的替换变量;在这里可能会过度使用,但对于更大的脚本中的清晰性是有用的:
define TAB_NAME=&1
select file_name,bytes/1024/1024,maxbytes/1024/1024
from dba_data_files
where tablespace_name=upper('&TAB_NAME');
/或者简化以后的引用:
define TAB_NAME=upper('&1')
select file_name,bytes/1024/1024,maxbytes/1024/1024
from dba_data_files
where tablespace_name=&TAB_NAME;
/注意,在这个版本中,对&TAB_NAME的引用不包含在单引号中,因为它们是在替换过程中应用的。
我可以在测试/调试时帮助set verify on,然后用set verify off来真正的隐藏噪音。您可能还会发现,在-s调用中包含sqlplus标志以隐藏横幅文本也是有用的。在本例中,您甚至不需要一个heredoc,您也可以直接从命令行运行脚本:
sqlplus -s "/ as sysdba" @$LOC/tab.sql $TAB_NAME(作为常规查询的sys连接并不理想;最好让一个拥有特权的普通用户查看您需要查询的数据字典表,但这是另一个主题.)
https://stackoverflow.com/questions/47325231
复制相似问题