【Oracle Database】指定フォルダ内の外部ファイルを都度接続なしで実行する方法

実行計画をリリース前リリース後に比較するようなツールを作成しているときに、評価したいSQLが複数の外部ファイルに別れていた

下記のような書き方では、SQL*PLUSで都度接続して外部ファイルを実行するようになる

```
#!/bin/bash

### CONF ###
exec_env="***"
user_id="***"
pass="***"
conn_str="***"
############

for file in `\find ./sql/release -maxdepth 1 -type f`; do
  echo "Reading ${file}"
  filename=`basename $file | sed 's/\.[^\.]*$//'`
sqlplus ${user_id}/${pass}@${conn_str} <<EOF --ここが毎回呼ばれる
  spo ./plan/release/${exec_env}/${filename}.txt
  @@${file}
  SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY());
  spo off
  exit
EOF
done
```

実行結果

```
[11:13:12 oracle@tscserver12 tool]$ ./release_out_plan.sh
Reading ./sql/release/dual2.sql

SQL*Plus: Release 12.1.0.2.0 Production on Tue May 28 11:13:13 2019

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Tue May 28 2019 11:12:55 +09:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options

SQL> SQL>
PL/SQL procedure successfully completed.


Explained.

SQL>
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 272002086

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected.

SQL> SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options
Reading ./sql/release/dual.sql

SQL*Plus: Release 12.1.0.2.0 Production on Tue May 28 11:13:13 2019

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Tue May 28 2019 11:13:13 +09:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options

SQL> SQL>
PL/SQL procedure successfully completed.


Explained.

SQL>
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3752461848

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     2 |     2   (0)| 00:00:01 |
|*  1 |  FILTER            |      |       |       |            |          |
|   2 |   TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------

   1 - filter(TO_NUMBER(:NUM)=1)

14 rows selected.

SQL> SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options
Reading ./sql/release/dual3.sql

SQL*Plus: Release 12.1.0.2.0 Production on Tue May 28 11:13:13 2019

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Tue May 28 2019 11:13:13 +09:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options

SQL> SQL>
Explained.

SQL>
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 272002086

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected.

SQL> SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options
```

こんな漢字でSQL*PLUSで実行したいコマンドを予め生成してSQL*PLUSにパイプで渡してやれば、1回の接続で指定フォルダ内の外部ファイルを一括実行することができる

```release_out_plan_1.sh
 #!/bin/bash

 ### CONF ###
 exec_env="***"
 user_id="***"
 pass="***"
 conn_str="***"
 ############

 sh ./release_out_plan_2.sh | sqlplus ${user_id}/${pass}@${conn_str}
 exit
```

```release_out_plan_2.sh
#!/bin/bash

### CONF ###
exec_env="***"
user_id="***"
pass="***"
conn_str="***"
############

for file in `\find ./sql/release -maxdepth 1 -type f`; do
  filename=`basename $file | sed 's/\.[^\.]*$//'`
  echo "set termout off"
  echo "spo ./plan/release/${exec_env}/${filename}.txt"
  echo "@@${file}"
  echo "SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY());"
  echo "spo off"
done
```

実行結果

```
[11:16:19 oracle@tscserver12 tool]$ ./release_out_plan_1.sh

SQL*Plus: Release 12.1.0.2.0 Production on Tue May 28 11:16:24 2019

Copyright (c) 1982, 2014, Oracle.  All rights reserved.

Last Successful login time: Tue May 28 2019 11:13:13 +09:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options

SQL> SQL> SQL> SQL>
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 272002086

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected.

SQL> SQL> SQL> SQL> SQL>
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 3752461848

---------------------------------------------------------------------------
| Id  | Operation          | Name | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |      |     1 |     2 |     2   (0)| 00:00:01 |
|*  1 |  FILTER            |      |       |       |            |          |
|   2 |   TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
---------------------------------------------------------------------------

Predicate Information (identified by operation id):

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------

   1 - filter(TO_NUMBER(:NUM)=1)

14 rows selected.

SQL> SQL> SQL> SQL> SQL>
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 272002086

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |     1 |     2 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| DUAL |     1 |     2 |     2   (0)| 00:00:01 |
--------------------------------------------------------------------------

8 rows selected.

SQL> SQL> Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Advanced Analytics and Real Application Testing options
```