シェルスクリプトから呼び出されたoracle sqlファイルに変数値を入力として渡します。

シェルスクリプトから呼び出されたoracle sqlファイルに変数値を入力として渡します。

「シェルスクリプトから呼び出された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"

おすすめ記事