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

Linux/Unix shell 參數傳遞到SQL腳本

在數據庫運維的過程中,Shell 腳本在很大程度上為運維提供了極大的便利性。而shell 腳本參數作為變量傳遞給SQL以及SQL腳本也是DBA經常碰到的情形之一。本文主要討論了如何將shell腳本的參數傳遞到SQL腳本之中並執行SQL查詢。

1、啟動sqlplus時執行腳本並傳遞參數

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. 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

# ----------------------------------------------

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 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、通過定義變量的方式來傳遞參數

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. 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" <<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, 'yyyymmddhh24') = '&begin_date'

AND TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&end_date';

exit;

查看本欄目更多精彩內容:http://www.bianceng.cn/OS/unix/

4、測試腳本

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

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定義的變量名相同

作者:51cto博客 Oracle小混子

Copyright © Linux教程網 All Rights Reserved