shell调用sqlplus查询oracle

[oracle@hb shell_test]$ cat echo_time 
#!/bin/sh

一.最简单的调用sqlplus
sqlplus -S "sys/unimas as sysdba" << !
select to_char(sysdate,'yyyy-mm-dd') today from dual;
exit;
!

[oracle@hb shell_test]$ ./echo_time 

TODAY
----------
2011-03-21

-S 是silent mode,不输出类似“SQL>”,连接数据库,关闭数据库之类的信息。


eof可以是任何字符串 比如"laldf"那么当你输入单独一行laldf时"shell认为输入结束,但是必须表示块开始必须使用<<;
开始和结束要匹配这个符号“<<”后面的内容
举例子:

[oracle@hb shell_test]$ sqlplus -s "sys/unimas as sysdba" << abc
> select to_char(sysdate,'yyyy-mm-dd') today from dual;
> exit;
> abc

TODAY
----------
2011-03-21

二.sqlplus的结果传递给shell的方法一

[oracle@hb shell_test]$ cat test2.sh 
#!/bin/bash
VALUE=`sqlplus -S "test/unimas" << !
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
select to_char(sysdate,'yyyy-mm-dd') today from dual;
exit
!`
echo $VALUE
if [ -n "$VALUE" ]; then
echo "The rows is $VALUE"
exit 0
else
echo "There is no row"
fi

三.sqlplus的结果传递给shell的方法二

[oracle@hb shell_test]$ cat test1.sh 
#!/bin/bash
sqlplus -S "test/unimas" << !
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
col coun new_value v_coun
select count(*) coun from lesson;
exit v_coun
!
VALUE="$?"
echo "show row:$VALUE"

col coun new_value v_coun v_coun为number类型。因为exit 只能返回数值类型。

四.把shell参数传递给sqlplus

#!/bin/bash
t_id="$1"
sqlplus -S "test/unimas" << !
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
select teachername from teacher where id=$t_id;
exit
!

五.sqlplus的结果存储在文件中

#!/bin/sh
sqlplus -S "test/unimas"<<EOF
set heading off
set feedback off
set pagesize 0
set verify off
set echo off
spool spool_file
SELECT * from teacher;
spool off
exit;
EOF

http://blog.chinaunix.net/space.php?uid=9124312&do=blog&id=181372

 

####################################################################################################################################

查看调度系统状态脚本:

#!/bin/sh

if [[ -z "$1" ]] || [[ "$1" -ne 0 && "$1" -ne 2 ]]       #使用[[ ]] 进行逻辑短路操作
then
    echo "Please input your parameter: query status[0,2]!"
    exit
fi

#for buname in cnlog enlog ItLog JrLog AuLog InnerLog
for buname in cnlog enlog 
do
    sqlplus -S 'etl/etl@dw_testdb' << abc        #使用 << EOF方式输入信息
    set line 155
    set pages 9999
    SELECT /*+ PARALLEL(a,4) */ * FROM $buname.hla_job_rec a where status = $1;
    exit
abc
done

 

 

在linux下用shell执行oracle的存储过程

 

####    函数  
####    说       明:执行oracle存储过程  
####    输入参数:{数据库tns}{数据库用户名}{数据库密码}{存储过程名}{批处理日期}  
####  输出参数:存储过程执行结果  
function exe_proc  
{  
    oracle_sid=$1  
    user_name=$2  
    user_pwd=$3  
    proc_name=$4  
    etl_date=$5  
error_code=`  
sqlplus -S -L /nolog<<EOF  
connect $user_pwd/$user_pwd@$oracle_sid  
set termout off;  
set echo off;  
set feedback off;  
set heading off;  
set pagesize 0;  
var ora_return_code number;  
call $proc_name($etl_date,:ora_return_code);  
select :ora_return_code from dual;  
quit  
EOF`  
echo    $error_code   
}  

 

问题:脚本内调用存储过程,脚本直接执行没问题,使用crontab 执行脚本存储过程未执行

 

原因:缺少oracle环境变量

 

解决:在shell脚本里添加oracle的环境变量

#!/bin/sh


PATH=$PATH:$HOME/bin    //环境变量

export ORACLE_BASE=/home/oracle         
export ORACLE_HOME=/home/oracle/product/11.2.0/client
export PATH=$PATH:$ORACLE_HOME/bin

szCfgFile=P_BROADBAND_YUYUEINFO_SMS.sql

cat<<EndCfg>$szCfgFile

Begin
P_BROADBAND_YUYUEINFO_SMS();
end;
/
exit
EndCfg
sqlplus -s uop_uec/D_5WI5KCCI0Z@nguecdb @$szCfgFile
rm $szCfgFile

 

posted @ 2018-04-11 14:10  Cslc-DaweiJ  阅读(1357)  评论(0)    收藏  举报