Linux/Unix shell 引數傳遞到SQL指令碼
在資料庫運維的過程中,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_profile
- fi
- if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ;then
- echo "Usage: "
-
echo " `basename $0`
" - 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
- robin@SZDB:~/dba_scripts/custom/awr> more tmp.sql
- 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;
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_profile
- fi
- if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ;then
- echo "Usage: "
-
echo " `basename $0`
" - 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 2: pass the parameter in SQL prompt. Using the same method with method 1
-
sqlplus -S " / as sysdba" <
- @/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date
- exit;
- EOF
- exit
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_profile
- fi
- if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ;then
- echo "Usage: "
-
echo " `basename $0`
" - 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 3: pass the parameter to global variable firstly.
-
sqlplus -S " / as sysdba" <
- 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
- robin@SZDB:~/dba_scripts/custom/awr> more 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;
4、測試指令碼
- robin@SZDB:~/dba_scripts/custom/awr> ./tmp.sh
- Usage:
-
tmp.sh
- 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
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定義的變數名相同
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23490154/viewspace-1062427/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Linux/Unix shell sql 之間傳遞變數LinuxSQL變數
- go 呼叫 shell 指令碼 如何傳遞引數Go指令碼
- shell (3)指令碼引數傳遞與數學運算指令碼
- Linux/Unix shell 指令碼中呼叫SQL,RMAN指令碼Linux指令碼SQL
- [linux shell]引數傳遞之空格Linux
- Shell學習【引數傳遞】
- [轉]Shell向Perl指令碼中傳遞變數的方法指令碼變數
- Shell自學二(引數傳遞和陣列)陣列
- shell程式引數傳遞給sqlplusSQL
- linux中main引數傳遞LinuxAI
- 使用python指令碼傳遞引數:(三種方式可收藏)Python指令碼
- 引數傳遞
- shell指令碼中main函式中$#獲取不到指令碼傳入引數個數淺析指令碼AI函式
- linux_shell awk中傳遞變數Linux變數
- shell指令碼的命令列引數 - todo指令碼命令列
- bash shell指令碼接受多個引數指令碼
- JNI傳遞引數
- Mybatis引數傳遞MyBatis
- python指令碼傳引數argvPython指令碼
- url傳遞的引數值編碼
- RestCloud ETL解決shell指令碼引數化RESTCloud指令碼
- shell的引數和指令碼流程改進指令碼
- Shell指令碼對ps命令隱藏引數指令碼
- mybatis傳遞引數到mapping.xmlMyBatisAPPXML
- Linux/Unix shell 指令碼清除歸檔日誌檔案Linux指令碼
- React事件傳遞引數React事件
- 路由元件傳遞引數路由元件
- SQL SERVER 2008傳遞表值引數SQLServer
- SQL Server 2008:傳遞表值引數SQLServer
- JS的方法引數傳遞(按值傳遞)JS
- C#引數傳遞之值引數C#
- 【pytest】使用parametrize將引數化變數傳遞到fixture變數
- Linux Shell指令碼Linux指令碼
- Linux shell 指令碼Linux指令碼
- 通過行為引數化傳遞程式碼
- shell指令碼變數和sqlplus變數傳遞採集資料入oracle資料庫指令碼變數SQLOracle資料庫
- Shell 中 $ 關於指令碼引數的幾種用法指令碼
- 請求引數的傳遞