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


相关内容

热门资讯

终于了解“顺欣茶楼有没有挂”详... 网上科普关于“顺欣茶楼有没有挂”话题很是火热,小编也是针对顺欣茶楼作*弊开挂的方法以及开挂对应的知识...
玩家攻略科普“十三十三水经典比... 家人们!今天小编来为大家解答十三十三水经典比鸡透视挂怎么安装这个问题咨询软件客服徽9784099的挂...
终于了解“风风棋牌有挂吗”透视... 终于了解“风风棋牌有挂吗”透视曝光猫腻您好,风风棋牌这个游戏其实有挂的,确实是有挂的,需要了解加客服...
【今日要闻】“新海贝之城拼三张... 您好:新海贝之城拼三张这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在...
终于明白“猜宝控制器辅助器”透... 有 亲,根据资深记者爆料猜宝控制器是可以开挂的,确实有挂(咨询软件无需打...
今日重磅消息“皖江欢乐麻将可以... 家人们!今天小编来为大家解答皖江欢乐麻将透视挂怎么安装这个问题咨询软件客服徽9752949的挂在哪里...
【第一财经】“闲逸斗地主是不是... 您好:闲逸斗地主这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游...
玩家攻略科普“乐斗小院辅助器?... 有 亲,根据资深记者爆料乐斗小院是可以开挂的,确实有挂(咨询软件无需打开...
重磅消息“衣兆丰悦到底是不是挂... 重磅消息“衣兆丰悦到底是不是挂?”(确实真的有挂)您好,衣兆丰悦这个游戏其实有挂的,确实是有挂的,需...
今日重大发现“新好游炸/金/花... 今日重大发现“新好游炸/金/花到底有挂吗?”(必胜开挂神器)您好,新好游炸/金/花这个游戏其实有挂的...