Linux/Unix shell 參數傳遞到SQL指令碼

來源:互聯網
上載者:User

      在資料庫營運的過程中,Shell 指令碼在很大程度上為營運提供了極大的便利性。而shell 指令碼參數作為變數傳遞給SQL以及SQL指令碼也是DBA經常碰到的情形之一。本文主要討論了如何將shell指令碼的參數傳遞到SQL指令碼之中並執行SQL查詢。
  有關shell與SQL之間的變數傳遞,請參考:  Linux/Unix shell sql 之間傳遞變數

1、啟動sqlplus時執行指令碼並傳遞參數

robin@SZDB:~/dba_scripts/custom/awr> more tmp.sh#!/bin/bash# ----------------------------------------------#  Set environment here#  Author : Robinson Cheng#  Blog   : http://blog.csdn.net/robinson_0612# ----------------------------------------------if [ -f ~/.bash_profile ]; then    . ~/.bash_profilefiif [ -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_dateelse    ORACLE_SID=${1}    begin_date=${2}    end_date=${3}fiexport ORACLE_SID begin_date end_date#Method 1: pass the parameter to script directly after script namesqlplus -S gx_adm/gx_adm @/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date exitrobin@SZDB:~/dba_scripts/custom/awr> more tmp.sqlSELECT 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;

2、在SQL提示符下傳遞參數

robin@SZDB:~/dba_scripts/custom/awr> more tmp2.sh#!/bin/bash# ----------------------------------------------#  Set environment here#  Author : Robinson Cheng#  Blog   : http://blog.csdn.net/robinson_0612# ----------------------------------------------if [ -f ~/.bash_profile ]; then    . ~/.bash_profilefiif [ -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_dateelse    ORACLE_SID=${1}    begin_date=${2}    end_date=${3}fiexport ORACLE_SID begin_date end_date#Method 2: pass the parameter in SQL prompt. Using the same method with method 1sqlplus -S " / as sysdba" <<EOF@/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_dateexit;EOFexit

3、通過定義變數的方式來傳遞參數

robin@SZDB:~/dba_scripts/custom/awr> more tmp3.sh #!/bin/bash# ----------------------------------------------#  Set environment here#  Author : Robinson Cheng#  Blog   : http://blog.csdn.net/robinson_0612# ----------------------------------------------if [ -f ~/.bash_profile ]; then    . ~/.bash_profilefiif [ -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_dateelse    ORACLE_SID=${1}    begin_date=${2}    end_date=${3}fiexport ORACLE_SID begin_date end_date#Method 3: pass the parameter to global variable firstly.sqlplus -S " / as sysdba" <<EOFdefine begin_date=$begin_date        define end_date=$end_dateprompt "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_dateexit;EOFexitrobin@SZDB:~/dba_scripts/custom/awr> more tmp3.sqlSELECT 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;

4、測試指令碼

robin@SZDB:~/dba_scripts/custom/awr> ./tmp.shUsage:       tmp.sh <ORACLE_SID> <begin_dat> <end_date>please input begin ORACLE_SID:CNMMBOplease input begin date and time(e.g. yyyymmddhh24):2013030709please input end date and time(e.g. yyyymmddhh24):2013030710   SNAP_ID       DBID SNAP_LEVEL---------- ---------- ----------     13877  938506715          1robin@SZDB:~/dba_scripts/custom/awr> ./tmp2.sh MMBOTST 2013030709 2013030710   SNAP_ID       DBID SNAP_LEVEL---------- ---------- ----------     36262 3509254984          1robin@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

5、小結
a、本文主要描述了將shell的參數傳遞給SQL指令碼
b、方式1的用法是直接將shell變數跟在指令碼之後, sqlplus userid/pwd @script_name $para1 $para2
c、方式2是啟動sqlplus後在SQL提示符下來傳遞參數, SQL>@script_name $para1 $para2
d、方式3則是將shell變數的值先傳遞給define定義的變數,然後再傳遞給SQL指令碼 SQL>@script_name var1 var2
e、注意方式3中SQL指令碼的替代變數與define定義的變數名相同

 

更多參考:

有關Oracle RAC請參考
     使用crs_setperm修改RAC資源的所有者及許可權
     使用crs_profile管理RAC資源設定檔
     RAC 資料庫的啟動與關閉
     再說 Oracle RAC services
     Services in Oracle Database 10g
     Migrate datbase from single instance to Oracle RAC
     Oracle RAC 串連到指定執行個體
     Oracle RAC 負載平衡測試(結合伺服器端與用戶端)
     Oracle RAC 伺服器端串連負載平衡(Load Balance)
     Oracle RAC 用戶端串連負載平衡(Load Balance)
     ORACLE RAC 下非預設連接埠監聽配置(listener.ora tnsnames.ora)
     ORACLE RAC 監聽配置 (listener.ora tnsnames.ora)
     配置 RAC 負載平衡與容錯移轉
     CRS-1006 , CRS-0215 故障一例 
     基於Linux (RHEL 5.5) 安裝Oracle 10g RAC
     使用 runcluvfy 校正Oracle RAC安裝環境

有關Oracle 網路設定相關基礎以及概念性的問題請參考:
     配置非預設連接埠的動態服務註冊
     配置sqlnet.ora限制IP訪問Oracle
     Oracle 監聽器日誌配置與管理
     設定 Oracle 監聽器密碼(LISTENER)
     配置ORACLE 用戶端串連到資料庫

有關基於使用者管理的備份和備份恢複的概念請參考
     Oracle 冷備份
     Oracle 熱備份
     Oracle 備份恢複概念
     Oracle 執行個體恢複
     Oracle 基於使用者管理恢複的處理
     SYSTEM 資料表空間管理及備份恢複
     SYSAUX資料表空間管理及恢複
     Oracle 基於備份控制檔案的恢複(unsing backup controlfile)

有關RMAN的備份恢複與管理請參考
     RMAN 概述及其體繫結構
     RMAN 配置、監控與管理
     RMAN 備份詳解
     RMAN 還原與恢複
     RMAN catalog 的建立和使用
     基於catalog 建立RMAN儲存指令碼
     基於catalog 的RMAN 備份與恢複
     RMAN 備份路徑困惑
     使用RMAN實現異機備份恢複(WIN平台)
     使用RMAN遷移檔案系統資料庫到ASM
     linux 下RMAN備份shell指令碼
     使用RMAN遷移資料庫到異機

有關ORACLE體繫結構請參考
     Oracle 資料表空間與資料檔案
     Oracle 密碼檔案
     Oracle 參數檔案
     Oracle 聯機重做記錄檔(ONLINE LOG FILE)
     Oracle 控制檔案(CONTROLFILE)
     Oracle 歸檔日誌
     Oracle 復原(ROLLBACK)和撤銷(UNDO)
     Oracle 資料庫執行個體啟動關閉過程
     Oracle 10g SGA 的自動化管理
     Oracle 執行個體和Oracle資料庫(Oracle體繫結構)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.