将多个txt文件导入到ORACLE

前端之家收集整理的这篇文章主要介绍了将多个txt文件导入到ORACLE前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

iamlaosong文

将文本文件导入到数据库,如果不需要对文件进行处理,用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,YLJB,YLZL,FCZDM,FCZ,JSZDM,JSZ,YJJDM,YJJ,JDJDM,JDJ,HBBH,LICHENG,PYJSFDM,PYJSF,PYJDSDM,PYJDS,PYJXSDM,PYJXS,PYJDM,PYJ,PYJGS,MARK)">>${CTLFILE}
    elif [ X"${tp}" == X"TD" ]; then
      echo "(JSRQ Date 'YYYYMMDD',PYDQ,SFTT,TDJSFDM,TDJSF,TDJDSDM,TDJDS,TDJXSDM,TDJXS,TDJDM,TDJ,TDJGS,SZFEILI,XZFEILI,MARK)">>${CTLFILE}
    elif [ X"${tp}" == X"ZY" ]; then
      echo "(JSRQ Date 'YYYYMMDD',CLJXSDM,CLJDM,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是循环变量,不会出现空值,但这种写法比较好,所以保留着。

参见:如何将CSV文件导入到ORACLE

原文链接:https://www.f2er.com/oracle/207575.html

猜你在找的Oracle相关文章