Linux/Unix shell 参数传递到SQL脚本
admin
2023-02-04 11:20:03
0

在数据库运维的过程中,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` "  

   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提示符下传递参数


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` "  

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

@/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` "  

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

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、测试脚本


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. 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、小结


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视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html


相关内容

热门资讯

德国总理:美国正在被伊朗羞辱 德国之声4月27日报道,德国总理默茨在访问一所学校时表示,在当前的持续冲突中,伊朗领导层正试图羞辱美...
理响中国|“长”歌以行,风云激... 光阴如梭,东方潮阔。这里是中国的长三角,世界的长三角。无论过去、现在还是未来,这片土地都因时代而生,...
白宫:特朗普及其国安团队开会讨... 新华社华盛顿4月27日电 美国白宫新闻秘书莱维特27日在记者会上证实,总统特朗普及其国家安全团队当天...
人民日报刊文:日本放开杀伤性武... 日本放开杀伤性武器出口推高地缘冲突风险(国际论坛)常思纯《人民日报》(2026年04月28日 第 0...
医疗保障法草案二审:明确生育保... 满足多样化健康保障需求本报记者 彭 波4月27日,医疗保障法草案二审稿提请十四届全国人大常委会第二十...
天津一景区发生自转旋翼机事故1... 澎湃新闻记者 吕新文中国民用航空华北地区管理局4月22日公布《豪客通航“10•1”天津长芦汉盐旅游区...
卡塔尔埃米尔与美国总统特朗普通... 当地时间24日,卡塔尔埃米尔塔米姆与美国总统特朗普通电话,重点就中东地区局势以及伊朗与美国谈判问题交...
男子30年前被扣押2859克黄... 澎湃新闻记者 王鑫家住辽宁省大连市的潘永嘉近日向澎湃新闻反映称,三十年前,他在大连周水子机场被盖州市...
商务部:取消反制欧盟两家金融机... 中华人民共和国商务部令二〇二六年 第1号鉴于欧盟已取消对中国两家金融机构的制裁措施,现公布《关于取消...
过去24小时共有5艘船只通过霍... 总台记者当地时间24日获悉,过去24小时内,共有5艘船只通过霍尔木兹海峡,其中包括一艘伊朗油轮。(总...