在數(shù)據(jù)庫(kù)運(yùn)維的過(guò)程中,Shell 腳本在很大程度上為運(yùn)維提供了極大的便利性。而shell 腳本參數(shù)作為變量傳遞給SQL以及SQL腳本也是DBA經(jīng)常碰到的情形之一。本文主要討論了如何將shell腳本的參數(shù)傳遞到SQL腳本之中并執(zhí)行SQL查詢。
1、啟動(dòng)sqlplus時(shí)執(zhí)行腳本并傳遞參數(shù)
robin@SZDB:~/dba_scripts/custom/awr> more 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. yyyymmddhh34):" begin_date
read -p "please input end date and time(e.g. yyyymmddhh34):" 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, 'yyyymmddhh34') = '&1'
AND TO_CHAR (end_interval_time, 'yyyymmddhh34') = '&2';
exit;
2、在SQL提示符下傳遞參數(shù)
robin@SZDB:~/dba_scripts/custom/awr> more tmp2.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. yyyymmddhh34):" begin_date
read -p "please input end date and time(e.g. yyyymmddhh34):" 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" <<EOF
@/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date
exit;
EOF
exit
3、通過(guò)定義變量的方式來(lái)傳遞參數(shù)
robin@SZDB:~/dba_scripts/custom/awr> more tmp3.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. yyyymmddhh34):" begin_date
read -p "please input end date and time(e.g. yyyymmddhh34):" 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" <<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
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, 'yyyymmddhh34') = '&begin_date'
AND TO_CHAR (end_interval_time, 'yyyymmddhh34') = '&end_date';
exit;
4、測(cè)試腳本
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. yyyymmddhh34):2013030709
please input end date and time(e.g. yyyymmddhh34):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、小結(jié)
a、本文主要描述了將shell的參數(shù)傳遞給SQL腳本
b、方式1的用法是直接將shell變量跟在腳本之后, sqlplus userid/pwd @script_name $para1 $para2
c、方式2是啟動(dòng)sqlplus后在SQL提示符下來(lái)傳遞參數(shù), SQL>@script_name $para1 $para2
d、方式3則是將shell變量的值先傳遞給define定義的變量,然后再傳遞給SQL腳本 SQL>@script_name var1 var2
e、注意方式3中SQL腳本的替代變量與define定義的變量名相同
oracle視頻教程請(qǐng)關(guān)注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文名稱:Linux/Unixshell參數(shù)傳遞到SQL腳本-創(chuàng)新互聯(lián)
瀏覽地址:http://aaarwkj.com/article40/pjgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開(kāi)發(fā)、用戶體驗(yàn)、網(wǎng)站改版、建站公司、網(wǎng)站設(shè)計(jì)公司、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容