将多个txt文件导入到ORACLE的方法

将文本文件导入到数据库,如果不需要对文件进行处理,用sqlldr导入不仅方便,而且速度快。下面这个导入脚本,通过循环生成每个文本文件导入的控制文件,其中X’09’表示分隔符是TAB符,然后再调用sqlldr命令进行导入。

#import datafile that unzip by timing job: jsunzip.sh
#eidt by iamlaosong 20170926
#usage: ./jsimp.sh 20170101 BCF
# $1=OPDATE $2=SYF|BCF

BAKDIR=/home/oracle/ems_suan
CTLFILE=control.ctl 
 
for tp in CK HK JK LY TD ZY 
do  
  tbname=EMSAPP_JS_${tp}_$2_TEST
  txtfile="${BAKDIR}/$1/$1_340000_1_${tp}_$2.txt"
  # process txtfile  
  echo  "${txtfile} --> ${tbname}"
  if [ -f ${txtfile} ]; then
    # generate control file
    echo load data>${CTLFILE}
    echo infile "'${txtfile}'">>${CTLFILE}
    echo append into table $tbname>>${CTLFILE}
    echo fields terminated by "X'09'">>${CTLFILE}
    # echo optionally enclosed by "'\"'">>${CTLFILE}
    if [ X"${tp}" == X"CK" ] || [ X"${tp}" == X"JK" ]; then
      echo "(JSRQ Date 'YYYYMMDD',ZBTM,YJTM,YJZLBH,YJZLMC,ZBZL,ZBZLMC,JDS,JSSX,ZLSX,JIFEIZL,JIHEZL,CLJSFDM,CLJSF,CLJDSDM,CLJDS,CLJXS,CLJ,CLJGS,SJJSFDM,SJJSF,SJJDSDM,SJJDS,SJJXSDM,SJJXS,SJJDM,SJJ,SJJGS,YCXX,FEILI,FEIYONG,MARK)">>${CTLFILE}
    elif [ X"${tp}" == X"HK" ] || [ X"${tp}" == X"LY" ]; then
      echo "(JSRQ Date 'YYYYMMDD',YLDM,YLMC,LDLSH,ZBTM,YJTM,YJZLBH,YJZLMC,ZBZL,ZBZLMC,YLJB,YLZL,FCZDM,FCZ,JSZDM,JSZ,YJJDM,YJJ,JDJDM,JDJ,JSSX,ZLSX,HBBH,LICHENG,JIFEIZL,JIHEZL,PYJSFDM,PYJSF,PYJDSDM,PYJDS,PYJXSDM,PYJXS,PYJDM,PYJ,PYJGS,SJJSFDM,SJJSF,SJJDSDM,SJJDS,SJJXSDM,SJJXS,SJJDM,SJJ,SJJGS,YCXX,FEILI,FEIYONG,MARK)">>${CTLFILE}
    elif [ X"${tp}" == X"TD" ]; then
      echo "(JSRQ Date 'YYYYMMDD',YJTM,YJZLBH,YJZLMC,ZBZL,ZBZLMC,JSSX,PYDQ,SFTT,JIFEIZL,JIHEZL,TDJSFDM,TDJSF,TDJDSDM,TDJDS,TDJXSDM,TDJXS,TDJDM,TDJ,TDJGS,SJJSFDM,SJJSF,SJJDSDM,SJJDS,SJJXSDM,SJJXS,SJJDM,SJJ,SJJGS,YCXX,SZFEILI,XZFEILI,FEIYONG,MARK)">>${CTLFILE}
    elif [ X"${tp}" == X"ZY" ]; then
      echo "(JSRQ Date 'YYYYMMDD',ZBTM,YJTM,YJZLBH,YJZLMC,ZBZL,ZBZLMC,JDS,JSSX,ZLSX,JIFEIZL,JIHEZL,CLJSFDM,CLJSF,CLJDSDM,CLJDS,CLJXSDM,CLJXS,CLJDM,CLJ,CLJGS,SJJSFDM,SJJSF,SJJDSDM,SJJDS,SJJXSDM,SJJXS,SJJDM,SJJ,SJJGS,YCXX,FEILI,FEIYONG,MARK)">>${CTLFILE}
    fi
    # import data
    sqlldr userid=emssxjk/emssxjk control=control.ctl skip=1
    cat control.log >> impsuan.log
    # read -p "txt file: press enter to continue..." var  
  fi
done

shell脚本中逻辑或的判断除了代码中的写法外,还可以采用-o的方法,即:

if [ X”${tp}” == X”CK” -o X”${tp}” == X”JK” ]; then

判断字符串中加上一个X是防止变量tp为空时出现语法错误,本例中tp是循环变量,不会出现空值,但这种写法比较好,所以保留着。

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐