如何将sqlplus中受影响的行数返回给shell脚本?

时间:2012-05-31 17:05:38

标签: oracle bash sqlplus

这是我的shell脚本:

# Deletes data from the 'sample' table starting August 30, 2011.
# This is done in stages with a 7 second break every 
# 2 seconds or so to free up the database for other users. 
# The message "Done." will be printed when there are 
# no database entries left to delete.

user="*****"
pass="*****"
while(true); do
    starttime=`date +%s`
    while [[ $((`date +%s` - $starttime)) -lt 2 ]]; do
        sqlplus $user/$pass@//blabla <<EOF
            whenever sqlerror exit 1
            delete from 
                sample
            where
                sampletime >= to_date('08-30-2011','mm-dd-yyyy') and
                rownum <= 2;
            commit;
EOF
        rows = ???
        if [ $rows -eq 0 ] ; then
        echo "Done."
        exit 0;
    fi
    done
    sleep 7
done

如果没有办法获得行数,也许我可以使用sqlplus返回的错误代码来确定何时结束脚本?有什么想法吗?

谢谢!

3 个答案:

答案 0 :(得分:1)

我使用sql%rowcount告诉你最后一个DML语句影响了多少行。

delete from                  
sample             
where                 
sampletime >= to_date('08-30-2011','mm-dd-yyyy') and                 
rownum <= 2;             


if sql%rowcount = 0 then
   dbms_output.put_line('Free');
end if;

commit; 

答案 1 :(得分:0)

请使用以下内容 注意:没有经过测试

user="*****" 
pass="*****" 
while(true); 
do     
starttime=`date +%s`     
while [[ $((`date +%s` - $starttime)) -lt 2 ]]; 
do         
  rows=`sqlplus -silent $user/$pass@//blabla << EOF             
  whenever sqlerror exit 1 
  SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF            
    select count(1) from                  
    sample             
    where                 
    sampletime >= to_date('08-30-2011','mm-dd-yyyy') and                 
    rownum <= 2;             

EOF`         

if [ $rows -neq 0] ; 
then 
    sqlplus $user/$pass@//blabla << EOF1  
    delete  from                  
    sample             
    where                 
    sampletime >= to_date('08-30-2011','mm-dd-yyyy') and                 
    rownum <= 2;             
    commit;
EOF1
else        
echo "Done."         
exit 0;     
fi     
done     
sleep 7 
done 

答案 2 :(得分:0)

请试试这个:

                    rows=`sqlplus -s ${DB_USER}/${DB_PASSWD}@${DB_SID} <<EOF
                    delete from sample where sampletime >= to_date('08-30-2011','mm-dd-yyyy') and rownum <= 2;
                    commit;
                    exit;

EOF`                 网络

            echo rows = $rows