Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Unix知識 >> Unix基礎知識

Linux/Unix Shell 參數傳遞到 SQL 腳本的三種寫法

在資料庫運維工作中,Shell 腳本大幅提升了效率;而如何把 Shell 變數當成參數傳進 SQL 腳本,更是 DBA 經常遇到的情境。本文整理三種可直接套用的做法,並以查詢 AWR 快照為例。

方法一:啟動 sqlplus 時直接帶入參數

Shell 腳本 tmp.sh:

#!/bin/bash
# ----------------------------------------------
#  Set environment here
#  Author : Robinson Cheng
# ----------------------------------------------
if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ]; then
    echo "Usage: "
    echo "      `basename $0` <ORACLE_SID> <begin_dat> <end_date>"
    read -p "please input begin ORACLE_SID:" ORACLE_SID
    read -p "please input begin date and time(e.g. yyyymmddhh24):" begin_date
    read -p "please input end date and time(e.g. yyyymmddhh24):" end_date
else
    ORACLE_SID=${1}
    begin_date=${2}
    end_date=${3}
fi
export ORACLE_SID begin_date end_date

# Method 1: pass the parameter to script directly after script name
sqlplus -S gx_adm/gx_adm @/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date
exit

SQL 腳本 tmp.sql 以 &1、&2 這類位置替代變數接收:

SELECT snap_id, dbid, snap_level
  FROM dba_hist_snapshot
 WHERE TO_CHAR(begin_interval_time, 'yyyymmddhh24') = '&1'
   AND TO_CHAR(end_interval_time, 'yyyymmddhh24') = '&2';
exit;

方法二:在 SQL 提示符下傳遞參數

改用 here document 把指令餵給 sqlplus,本質與方法一相同:

#!/bin/bash
# ...(環境設定與參數檢查同上,略)...
export ORACLE_SID begin_date end_date

# Method 2: pass the parameter in SQL prompt
sqlplus -S " / as sysdba" <<EOF
@/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date
exit;
EOF
exit

方法三:先定義全域變數再傳入

用 define 把 Shell 變數轉成 SQL 全域替代變數,SQL 腳本內改用具名變數,可讀性最好:

#!/bin/bash
# ...(環境設定與參數檢查同上,略)...
export ORACLE_SID begin_date end_date

# Method 3: pass the parameter to global variable firstly.
sqlplus -S " / as sysdba" <<EOF
define begin_date=$begin_date
define end_date=$end_date
prompt "variable value for begin_date is: &begin_date"
prompt "variable value for end_date id : &end_date"
@/users/robin/dba_scripts/custom/awr/tmp3.sql begin_date end_date
exit;
EOF
exit

對應的 tmp3.sql:

SELECT snap_id, dbid, snap_level
  FROM dba_hist_snapshot
 WHERE TO_CHAR(begin_interval_time, 'yyyymmddhh24') = '&begin_date'
   AND TO_CHAR(end_interval_time, 'yyyymmddhh24') = '&end_date';
exit;

實際測試

robin@SZDB:~/dba_scripts/custom/awr> ./tmp.sh
Usage:
  tmp.sh <ORACLE_SID> <begin_dat> <end_date>
please input begin ORACLE_SID:CNMMBO
please input begin date and time(e.g. yyyymmddhh24):2013030709
please input end date and time(e.g. yyyymmddhh24):2013030710

SNAP_ID      DBID SNAP_LEVEL
---------- ---------- ----------
     13877  938506715          1

robin@SZDB:~/dba_scripts/custom/awr> ./tmp2.sh MMBOTST 2013030709 2013030710
SNAP_ID      DBID SNAP_LEVEL
---------- ---------- ----------
     36262 3509254984          1

robin@SZDB:~/dba_scripts/custom/awr> ./tmp3.sh MMBOTST 2013030710 2013030711
"variable value for begin_date is: 2013030710"
"variable value for end_date id : 2013030711"

SNAP_ID      DBID SNAP_LEVEL
---------- ---------- ----------
     36263 3509254984          1

小結

  • 方法一:sqlplus userid/pwd @script_name $para1 $para2,參數緊跟在腳本名之後,SQL 內用 &1、&2。
  • 方法二:先進入 sqlplus,在 SQL 提示符下下 @script_name $para1 $para2。
  • 方法三:先把 Shell 變數值餵給 define 定義的變數,再下 @script_name var1 var2。
  • 方法三要特別注意:SQL 腳本中的替代變數名稱必須與 define 的變數名一致。
  • 三種方法都建議保留參數檢查與 read 互動輸入的分支,讓腳本在缺少參數時仍可人工補齊,避免誤查全庫。
Copyright © Linux教程網 All Rights Reserved