想知道是否可以“将变量值作为输入传递给从 shell 脚本调用的 oracle sql 文件”
我正在尝试这样,但它不起作用:
SQL 文件:
set head on
set feed on
set termout off
set trimspool on
spool ts_verify.log
select tablespace_name from dba_tablespaces where tablespace_name='$ts_name';
spool off
set termout on
set trimspool off
exit
外壳脚本:
ts_name=''
echo "Please enter the Tablespace Name for which you want to add datafile"
read ts_name
sqlplus -s "/ as sysdba" << EOF
@ts.sql
EXIT
EOF
问候, 阿迪亚
答案1
将 SQL 脚本转换为模板文件,其中包含以下占位符真的易于搭配。然后,当您知道变量的值时,从模板创建真正的 SQL 脚本:
template.sql
:
set head on
set feed on
set termout off
set trimspool on
spool ts_verify.log
select tablespace_name from dba_tablespaces where tablespace_name='@@TABLESPACENAME@@';
spool off
set termout on
set trimspool off
exit
你的脚本:
#!/bin/sh
unset ts_name
echo "Please enter the Tablespace Name for which you want to add datafile"
read -r ts_name
# create real SQL script and pass it to `sqlplus`:
sed "s/@@TABLESPACENAME@@/$ts_name/g" template.sql |
sqlplus -s "/ as sysdba"