前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux/Unix shell 参数传递到SQL脚本

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

作者头像
Leshami
发布2018-08-14 10:20:02
1.9K0
发布2018-08-14 10:20:02
举报
文章被收录于专栏:乐沙弥的世界乐沙弥的世界

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

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

代码语言:javascript
复制
robin@SZDB:~/dba_scripts/custom/awr> more tmp.sh
#!/bin/bash

# ----------------------------------------------
#  Set environment here
#  Author : Robinson Cheng
#  Blog   : http://blog.csdn.net/robinson_0612
# ----------------------------------------------

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

代码语言:javascript
复制
robin@SZDB:~/dba_scripts/custom/awr> more tmp2.sh
#!/bin/bash

# ----------------------------------------------
#  Set environment here
#  Author : Robinson Cheng
#  Blog   : http://blog.csdn.net/robinson_0612
# ----------------------------------------------

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、通过定义变量的方式来传递参数

代码语言:javascript
复制
robin@SZDB:~/dba_scripts/custom/awr> more tmp3.sh 
#!/bin/bash

# ----------------------------------------------
#  Set environment here
#  Author : Robinson Cheng
#  Blog   : http://blog.csdn.net/robinson_0612
# ----------------------------------------------

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;

4、测试脚本

代码语言:javascript
复制
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定义的变量名相同

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013年03月07日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档