Shell

將變數值作為輸入傳遞給從 shell 腳本呼叫的 oracle sql 文件

  • April 27, 2018

想知道是否可以“將變數值作為輸入傳遞給從 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

問候,阿迪亞

將您的 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"

引用自:https://unix.stackexchange.com/questions/440313