Linux/Unix shell 参数传递到SQL脚本
作者: 发布时间:[ 2013/6/20 10:35:18 ] 推荐标签:
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_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、测试脚本
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定义的变量名相同
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11