LINUX使用SHELL对DB2数据库中的大表中的非月末数据进行分离

#!/bin/sh

. ./public_s.sh

tdtp="zxjxhisdsspace1";  #tablespace
titp="zxjxhisidxspace1"; #index tablespace 

tbnm="plat_accinfosh";
tb=$tbnm"_his";
getlogname $0;
log=$pub_result;

[ "$1" != "" ] && tbnm=$1;

pub_datatype="D";

strdate=20130101
enddate=20130228

[ "$2" != "" ] && strdate=$2;
[ "$3" != "" ] && enddate=$3;

#获取两日间数据日期
fc_getdayebtw  $strdate $enddate;
days="$pub_result";

#获取两月间数据日期
pub_datatype="M";
fc_getdayebtw  $strdate $enddate;
mdays=$pub_result;


tbexist="N";

pub_debug="N";

#判断是否存在表
callchktb()
{
  chtb=$1;
  
  if [ "$chtb" = "" ]
  then 
    echo "chtb is empty !exit!";
    exit;
  fi;
  
  if [ "$tbexist" = "N" ]
  then
     #检查表是否存在,表的格式为$tbname+his
     
     sql="select count(1) from syscat.tables a where a.tabname=upper('$chtb')";
     echo "sql is [$sql]" | tee -a $log;
     db2cmd "$sql";
     [ "$pub_result" = "1" ] && tbexist="Y";
     
echo "chtb is $chtb and pub_result [$pub_result] ";
  fi;
}

#创建表
callcreate()
{
    createtsql=" create table $ctbnm like $tbnm in $tdtp index in $titp";
    echo "$createtsql" | tee -a $log;
    db2cmd "$createtsql";
    echo "$pub_result" | tee -a $log;
    echo "$pub_sqlcode" | tee -a $log;
}


#插入函数
callinsert()
{
  cdate=$1;
  ctbnm=$2;
  
  if [ "$cdate" = "" -o "$ctbnm" = "" ]
  then
     echo "cdate or ctbnm is empty" | tee -a $log;
     exit;
  fi;

  #检查表是否存在,表的格式为$tbname+his
  callchktb $ctbnm;
  #echo "1........"
  if [ "$tbexist" = "N" ]
  then
    callcreate $ctbnm;
    #echo "10........"
  fi;
  callchktb $ctbnm;
  #echo "2........"
  
  if [ "$tbexist" = "Y" ]
  then
  #echo "3........"
    insertsql=" insert into $ctbnm 
                select * from $tbnm where datadate=$cdate 
                 and not exists ( select 1 from $ctbnm where datadate=$cdate fetch first 1 rows only)
                 with ur";
    echo "$insertsql" | tee -a $log;
    db2cmd "$insertsql" ;
    echo "$pub_result" | tee -a $log;
    echo "$pub_sqlcode" | tee -a $log;
  fi;
  
  detesql="delete from $tbnm where datadate=$cdate and exists ( select 1 from $ctbnm where datadate=$cdate)"
  db2cmd "$detesql";
  echo "$detesql" | tee -a $log;
  echo "$pub_result" | tee -a $log; 
  echo "$pub_sqlcode" | tee -a $log;
} 

#echo days is [$days]
#echo mdays is [$mdays]
#exit;
for d in $days
do
  #去除每个月底数据,其他的数据插入到历史表中
  for f2d in $mdays
  do          
    [ "$d" = "$f2d" ] && continue 2;
  done;
  echo callinsert $d $tb;
  callinsert $d $tb;
done
db2cmd "runstats on table zxjx.$tb";

 

posted @ 2020-04-04 15:22  silencemaker1221  阅读(206)  评论(0编辑  收藏  举报