Linux/Unix shell 参数传递到SQL脚本

在数据库运维的过程中,Shell 脚本在很大程度上为运维提供了极大的便利性。而shell 脚本参数作为变量传递给SQL以及SQL脚本也是DBA经常碰到的情形之一。本文主要讨论了如何将shell脚本的参数传递到SQL脚本之中并执行SQL查询。
  有关shell与SQL之间的变量传递,请参考:  Linux/Unix shell sql 之间传递变量

1、启动sqlplus时执行脚本并传递参数

[python]  view plain copy print ?
  1. robin@SZDB:~/dba_scripts/custom/awr> more tmp.sh  
  2. #!/bin/bash  
  3.   
  4. # ----------------------------------------------  
  5. #  Set environment here  
  6. #  Author : Robinson Cheng  
  7. #  Blog   : http://blog.csdn.net/robinson_0612  
  8. # ----------------------------------------------  
  9.   
  10. if [ -f ~/.bash_profile ]; then  
  11.     . ~/.bash_profile  
  12. fi  
  13.   
  14. if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ;then  
  15.     echo "Usage: "  
  16.     echo "      `basename $0` <ORACLE_SID> <begin_dat> <end_date>"  
  17.     read -p "please input begin ORACLE_SID:" ORACLE_SID  
  18.     read -p "please input begin date and time(e.g. yyyymmddhh24):" begin_date  
  19.     read -p "please input end date and time(e.g. yyyymmddhh24):" end_date  
  20. else  
  21.     ORACLE_SID=${1}  
  22.     begin_date=${2}  
  23.     end_date=${3}  
  24. fi  
  25.   
  26. export ORACLE_SID begin_date end_date  
  27.   
  28. #Method 1: pass the parameter to script directly after script name  
  29. sqlplus -S gx_adm/gx_adm @/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date   
  30.   
  31. exit  
  32.   
  33. robin@SZDB:~/dba_scripts/custom/awr> more tmp.sql  
  34. SELECT snap_id, dbid, snap_level  
  35.   FROM dba_hist_snapshot  
  36.  WHERE TO_CHAR (begin_interval_time, 'yyyymmddhh24') = '&1'  
  37.        AND TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&2';  
  38. exit;  

2、在SQL提示符下传递参数

[python]  view plain copy print ?
  1. robin@SZDB:~/dba_scripts/custom/awr> more tmp2.sh  
  2. #!/bin/bash  
  3.   
  4. # ----------------------------------------------  
  5. #  Set environment here  
  6. #  Author : Robinson Cheng  
  7. #  Blog   : http://blog.csdn.net/robinson_0612  
  8. # ----------------------------------------------  
  9.   
  10. if [ -f ~/.bash_profile ]; then  
  11.     . ~/.bash_profile  
  12. fi  
  13.   
  14. if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ;then  
  15.     echo "Usage: "  
  16.     echo "      `basename $0` <ORACLE_SID> <begin_dat> <end_date>"  
  17.     read -p "please input begin ORACLE_SID:" ORACLE_SID  
  18.     read -p "please input begin date and time(e.g. yyyymmddhh24):" begin_date  
  19.     read -p "please input end date and time(e.g. yyyymmddhh24):" end_date  
  20. else  
  21.     ORACLE_SID=${1}  
  22.     begin_date=${2}  
  23.     end_date=${3}  
  24. fi  
  25.   
  26. export ORACLE_SID begin_date end_date  
  27.   
  28. #Method 2: pass the parameter in SQL prompt. Using the same method with method 1  
  29. sqlplus -S " / as sysdba" <<EOF  
  30. @/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date  
  31. exit;  
  32. EOF  
  33. exit  

3、通过定义变量的方式来传递参数

[python]  view plain copy print ?
  1. robin@SZDB:~/dba_scripts/custom/awr> more tmp3.sh   
  2. #!/bin/bash  
  3.   
  4. # ----------------------------------------------  
  5. #  Set environment here  
  6. #  Author : Robinson Cheng  
  7. #  Blog   : http://blog.csdn.net/robinson_0612  
  8. # ----------------------------------------------  
  9.   
  10. if [ -f ~/.bash_profile ]; then  
  11.     . ~/.bash_profile  
  12. fi  
  13.   
  14. if [ -z "${1}" ] || [ -z "${2}" ] || [ -z "${3}" ] ;then  
  15.     echo "Usage: "  
  16.     echo "      `basename $0` <ORACLE_SID> <begin_dat> <end_date>"  
  17.     read -p "please input begin ORACLE_SID:" ORACLE_SID  
  18.     read -p "please input begin date and time(e.g. yyyymmddhh24):" begin_date  
  19.     read -p "please input end date and time(e.g. yyyymmddhh24):" end_date  
  20. else  
  21.     ORACLE_SID=${1}  
  22.     begin_date=${2}  
  23.     end_date=${3}  
  24. fi  
  25.   
  26. export ORACLE_SID begin_date end_date  
  27.   
  28. #Method 3: pass the parameter to global variable firstly.  
  29. sqlplus -S " / as sysdba" <<EOF  
  30. define begin_date=$begin_date          
  31. define end_date=$end_date  
  32. prompt "variable value for begin_date is: &begin_date"  
  33. prompt "variable value for end_date id : &end_date"  
  34. @/users/robin/dba_scripts/custom/awr/tmp3.sql begin_date end_date  
  35. exit;  
  36. EOF  
  37. exit  
  38.   
  39. robin@SZDB:~/dba_scripts/custom/awr> more tmp3.sql  
  40. SELECT snap_id, dbid, snap_level  
  41.   FROM dba_hist_snapshot  
  42.  WHERE TO_CHAR (begin_interval_time, 'yyyymmddhh24') = '&begin_date'  
  43.        AND TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&end_date';  
  44. exit;  

4、测试脚本

[python]  view plain copy print ?
  1. robin@SZDB:~/dba_scripts/custom/awr> ./tmp.sh  
  2. Usage:   
  3.       tmp.sh <ORACLE_SID> <begin_dat> <end_date>  
  4. please input begin ORACLE_SID:CNMMBO  
  5. please input begin date and time(e.g. yyyymmddhh24):2013030709  
  6. please input end date and time(e.g. yyyymmddhh24):2013030710  
  7.   
  8.    SNAP_ID       DBID SNAP_LEVEL  
  9. ---------- ---------- ----------  
  10.      13877  938506715          1  
  11.   
  12. robin@SZDB:~/dba_scripts/custom/awr> ./tmp2.sh MMBOTST 2013030709 2013030710  
  13.   
  14.    SNAP_ID       DBID SNAP_LEVEL  
  15. ---------- ---------- ----------  
  16.      36262 3509254984          1  
  17.   
  18. robin@SZDB:~/dba_scripts/custom/awr> ./tmp3.sh MMBOTST 2013030710 2013030711  
  19. "variable value for begin_date is: 2013030710"  
  20. "variable value for end_date id : 2013030711"  
  21.   
  22.    SNAP_ID       DBID SNAP_LEVEL  
  23. ---------- ---------- ----------  
  24.      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定义的变量名相同

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值