使用函数和SQL查询编写unix shell脚本时使用一次数据库访问

时间:2017-05-06 13:51:54

标签: sql oracle unix ksh

我开发了一个脚本,可以根据特定条件从表中删除记录(包含数百万条记录)。下面是脚本。但我的要求是使用一个调用数据库访问的实例。在这个脚本中,我使用了两次访问。我不知道怎样才能用一次性数据库访问来编写它,这将进行必要的删除和选择;

#!/bin/ksh
#set -u
log()
{

   local date='date +"%b %d %Y  %T"'
   echo "$date [log]       : $*." | tee -a $logfile

}


declaration()

{
   date='date +"%b_%d_%Y_%T"'
   LOGPATH=/opt/RP/abcd/
   logfile=$LOGPATH"record_"$date

}


checkstatus ()

{
COUNT=`sqlplus -s $ORACLE_USR <<END // using the database access for the first time
set pagesize 0 feedback off verify off heading off echo off;
SELECT  count(*) FROM name WHERE age  not in('12,'24,'38','50') AND trunc(dstamp)=trunc(sysdate-10) ;
exit;
END`

log "$COUNT records found "
}

deleterecord ()
{
sqlplus -s $ORACLE_USR  <<END  // using the database access for the second time
DELETE from name WHERE age  not in('12,'24,'38','50') AND trunc(dstamp)=trunc(sysdate-10) 
AND Rownum <=30000;
COMMIT;
EXIT;
END
}

declaration
checkstatus

while [ $COUNT -gt 0 ]
do
deleterecord
COUNT=$(( $COUNT - 30000 ))
if [ $COUNT -lt 0 ]
then
log "All records are deleted"
else
log "$COUNT records remaining in table"
fi
done

0 个答案:

没有答案