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

来源:互联网 发布:红旗linux桌面版 编辑:程序博客网 时间:2024/05/01 17:58

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

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

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_profilefiif [ -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_dateelse    ORACLE_SID=${1}    begin_date=${2}    end_date=${3}fiexport ORACLE_SID begin_date end_date#Method 1: pass the parameter to script directly after script namesqlplus -S gx_adm/gx_adm @/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_date exitrobin@SZDB:~/dba_scripts/custom/awr> more tmp.sqlSELECT 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#  Blog   : http://blog.csdn.net/robinson_0612# ----------------------------------------------if [ -f ~/.bash_profile ]; then    . ~/.bash_profilefiif [ -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_dateelse    ORACLE_SID=${1}    begin_date=${2}    end_date=${3}fiexport ORACLE_SID begin_date end_date#Method 2: pass the parameter in SQL prompt. Using the same method with method 1sqlplus -S " / as sysdba" <<EOF@/users/robin/dba_scripts/custom/awr/tmp.sql $begin_date $end_dateexit;EOFexit

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

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_profilefiif [ -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_dateelse    ORACLE_SID=${1}    begin_date=${2}    end_date=${3}fiexport ORACLE_SID begin_date end_date#Method 3: pass the parameter to global variable firstly.sqlplus -S " / as sysdba" <<EOFdefine begin_date=$begin_date        define end_date=$end_dateprompt "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_dateexit;EOFexitrobin@SZDB:~/dba_scripts/custom/awr> more tmp3.sqlSELECT 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、测试脚本

robin@SZDB:~/dba_scripts/custom/awr> ./tmp.shUsage:       tmp.sh <ORACLE_SID> <begin_dat> <end_date>please input begin ORACLE_SID:CNMMBOplease input begin date and time(e.g. yyyymmddhh24):2013030709please input end date and time(e.g. yyyymmddhh24):2013030710   SNAP_ID       DBID SNAP_LEVEL---------- ---------- ----------     13877  938506715          1robin@SZDB:~/dba_scripts/custom/awr> ./tmp2.sh MMBOTST 2013030709 2013030710   SNAP_ID       DBID SNAP_LEVEL---------- ---------- ----------     36262 3509254984          1robin@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 RAC请参考
     使用crs_setperm修改RAC资源的所有者及权限
     使用crs_profile管理RAC资源配置文件
     RAC 数据库的启动与关闭
     再说 Oracle RAC services
     Services in Oracle Database 10g
     Migrate datbase from single instance to Oracle RAC
     Oracle RAC 连接到指定实例
     Oracle RAC 负载均衡测试(结合服务器端与客户端)
     Oracle RAC 服务器端连接负载均衡(Load Balance)
     Oracle RAC 客户端连接负载均衡(Load Balance)
     ORACLE RAC 下非缺省端口监听配置(listener.ora tnsnames.ora)
     ORACLE RAC 监听配置 (listener.ora tnsnames.ora)
     配置 RAC 负载均衡与故障转移
     CRS-1006 , CRS-0215 故障一例 
     基于Linux (RHEL 5.5) 安装Oracle 10g RAC
     使用 runcluvfy 校验Oracle RAC安装环境

有关Oracle 网络配置相关基础以及概念性的问题请参考:
     配置非默认端口的动态服务注册
     配置sqlnet.ora限制IP访问Oracle
     Oracle 监听器日志配置与管理
     设置 Oracle 监听器密码(LISTENER)
     配置ORACLE 客户端连接到数据库

有关基于用户管理的备份和备份恢复的概念请参考
     Oracle 冷备份
     Oracle 热备份
     Oracle 备份恢复概念
     Oracle 实例恢复
     Oracle 基于用户管理恢复的处理
     SYSTEM 表空间管理及备份恢复
     SYSAUX表空间管理及恢复
     Oracle 基于备份控制文件的恢复(unsing backup controlfile)

有关RMAN的备份恢复与管理请参考
     RMAN 概述及其体系结构
     RMAN 配置、监控与管理
     RMAN 备份详解
     RMAN 还原与恢复
     RMAN catalog 的创建和使用
     基于catalog 创建RMAN存储脚本
     基于catalog 的RMAN 备份与恢复
     RMAN 备份路径困惑
     使用RMAN实现异机备份恢复(WIN平台)
     使用RMAN迁移文件系统数据库到ASM
     linux 下RMAN备份shell脚本
     使用RMAN迁移数据库到异机

有关ORACLE体系结构请参考
     Oracle 表空间与数据文件
     Oracle 密码文件
     Oracle 参数文件
     Oracle 联机重做日志文件(ONLINE LOG FILE)
     Oracle 控制文件(CONTROLFILE)
     Oracle 归档日志
     Oracle 回滚(ROLLBACK)和撤销(UNDO)
     Oracle 数据库实例启动关闭过程
     Oracle 10g SGA 的自动化管理
     Oracle 实例和Oracle数据库(Oracle体系结构)