当前位置:数据库 > Oracle >>

oracle的文本导入、导出技巧(参照informix的load,unload)

答案:在使用oracle时,总觉得oracle的导入,导出没有informix的load,unload好用,没办法,只能参照网上朋友的思路和informix的实现,写了几个脚本,希望各位高手指点:

  =============================================

  工具用法:

  unload.sh

  Usage: unload.sh userid/passwd[@oraclesid] [tabname]

  注释:

  如果不加’@oraclesid’,则用当前环境变量ORACLE_SID。

  如果不加tabname参数,则处理该用户下的所有表,生成’表名.unl’文件。

  如果加tabname参数,则单独处理该表,生成’表名.unl’文件。

  load.sh

  Usage: load.sh userid/passwd[@oraclesid] [tabname]

  注释:

  如果不加’@oraclesid’,则用当前环境变量ORACLE_SID。

  如果不加tabname参数,则装载该用户下的所有表,必须存在’表名.unl’

  文件。

  如果加tabname参数,则单独处理该表,必须存在’表名.unl’文件。

  =============================================

  具体实现:

  unload.sh

  利用spool的功能,将表中的数据导出到对应的文本中。

  load.sh

  利用sql*load的功能,将文本中的数据导入到表中。

  控制文件是自动生成的,使用者不需要知道格式

  执行sqlload的脚本也是自动生成

  generate_control_file.sh

  生成控制文件

  generate_execute_shell.sh

  生成执行脚本

  =============================================

  代码说明

  oad.sh

  #!/usr/bin/ksh

  ################################################################################

  #

  # 模块: load.sh

  #

  # 描述: 根据一个数据库或表名来装载对应的数据文件

  #

  # 参数 1 = 用户名/密码[@实例名]

  # 参数 2 = 表名(可选)

  #

  # 作者 Bing He

  #

  # 修改记录

  # 日期 修改人 修改描述

  #

  # 09/23/2003 Bing He 开始编写

  #

  ################################################################################

  lv_temp1="wk.test1"

  f_get_tables()

  {

  rm -f ${lv_temp1}

  sqlplus ${userid} <<! >/dev/null

  set colsep $sep;

  set echo off;

  set feedback off;

  set heading off;

  set pagesize 0;

  set linesize 1000;

  set numwidth 12;

  set termout off;

  set trimout on;

  set trimspool on;

  spool ${lv_temp1};

  select table_name from user_tables;

  spool off;

  exit

  !

  if [ "$?" -ne 0 ]

  then

  echo "Error:sqlplus ${userid} error in load for ${userid} !"

  echo "please check userid and passwd or oracle_sid."

  exit

  fi

  if [ -f ${lv_temp1} ]

  then

  lv_tables=`cat ${lv_temp1} |grep -v "^SQL>" | tr -d ' '`

  else

  echo "Error:${lv_temp1} file not found!"

  exit

  fi

  rm -f ${lv_temp1}

  }

  ################################################################################

  ## 主程序入口

  lv_no=$#

  case ${lv_no} in

  1

  userid=$1

  f_get_tables;

  ;;

  2

  userid=$1

  lv_tables=$2

  ;;

  *

  echo "Usage: $0 <userid/passwd[@connection]> <table_name>"

  exit

  ;;

  esac

  for lv_table in ${lv_tables}

  do

  if [ ! -f ${lv_table}.unl ]

  then

  echo "Error:${lv_table}.unl file not found!"

  else

  generate_control_file.sh ${userid} ${lv_table}

  generate_execute_shell.sh ${userid} ${lv_table}

  sh load_${lv_table}.sh

  rm -f ${lv_table}.ctl

  rm -f load_${lv_table}.sh

  fi

  done

  ################################################################################

  unload.sh

  #!/usr/bin/ksh

  ################################################################################

  #

  # 模块: unload.sh

  #

  # 描述: 根据一个数据库或表名来卸载数据并生成对应的数据文件

  #

  # 参数 1 = 用户名/密码[@实例名]

  # 参数 2 = 表名(可选)

  #

  # 作者 Bing He

  #

  # 修改记录

  # 日期 修改人 修改描述

  #

  # 09/23/2003 Bing He 开始编写

  #

  ################################################################################

  lv_sep='|' # --分隔符,可以修改成自己想要的分隔符,如'|'

  lv_temp1="unload.temp1"

  f_get_tables()

  {

  rm -f ${lv_temp1}

  sqlplus ${userid} <<! >/dev/null

  set colsep ${lv_sep};

  set echo off;

  set feedback off;

  set heading off;

  set pagesize 0;

  set linesize 1000;

  set numwidth 12;

  set termout off;

  set trimout on;

  set trimspool on;

  spool ${lv_temp1};

  select table_name from user_tables;

  spool off;

  exit

  !

  if [ "$?" -ne 0 ] ; then

  echo "sqlplus $userid error in get table name <"$?">!!"

  echo "please check userid and passwd or database."

  exit

  fi

  if [ -f ${lv_temp1} ]

  then

  lv_tables=`cat ${lv_temp1} |grep -v "^SQL>" | tr -d ' '`

  else

  echo "Error:${lv_temp1} file not found!"

  exit

  fi

  rm -f ${lv_temp1}

  }

  ################################################################

  ## 主程序入口

  lv_no=$#

  case ${lv_no} in

  1

  userid=$1

  f_get_tables;

  ;;

  2

  userid=$1

  lv_tables=$2

  ;;

  *

  echo "Usage: $0 <userid/passwd[@connection]> <table_name>"

  exit

  ;;

  esac

  ################################################################

  ################################################################

  ## 执行下载操作

  for table in ${lv_tables}

  do

  rm -f lv_$table.txt

  sqlplus ${userid} <<! >/dev/null

  set colsep ${lv_sep};

  set echo off;

  set feedback off;

  set heading off;

  set pagesize 0;

  set linesize 1000;

  set numwidth 12;

  set termout off;

  set trimout on;

  set trimspool on;

  spool lv_$table.txt;

  select * from $table;

  spool off;

  !

  if [ "$?" -ne 0 ]

  then

  echo "error:sqlplus $userid error in unload table $table!!"

  echo "please check us

上一个:生成带日期的文件名
下一个:ORACLE数据库备份技术

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,