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

php中文网
发布: 2016-06-07 16:46:53
原创
1627人浏览过

在数据库运维的过程中,Shell 脚本在很大程度上为运维提供了极大的便利性。而shell 脚本参数作为变量传递给SQL以及SQL脚本也是DB

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

if [ -f ~/.bash_profile ]; then
    . ~/.bash_profile
fi

MacsMind
MacsMind

电商AI超级智能客服

MacsMind 192
查看详情 MacsMind

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. 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;

linux

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号