• perl unload utf-8 oracle 数据库


    perl unload utf-8  Oracle
    
    [oracle@oadb sbin]$ sqlplus / as sysdba
    
    SQL*Plus: Release 11.2.0.4.0 Production on 星期四 11月 17 18:03:12 2016
    
    Copyright (c) 1982, 2013, Oracle.  All rights reserved.
    
    
    连接到: 
    Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
    With the Partitioning, OLAP, Data Mining and Real Application Testing options
    
    SQL>  select userenv('language') from dual;
    
    USERENV('LANGUAGE')
    ----------------------------------------------------
    SIMPLIFIED CHINESE_CHINA.AL32UTF8
    
    
    SQL>  select * from nls_database_parameters where parameter='NLS_CHARACTERSET';  
    
    PARAMETER
    ------------------------------
    VALUE
    --------------------------------------------------------------------------------
    NLS_CHARACTERSET
    AL32UTF8
    
    [oracle@oadb sbin]$ echo $LANG
    en_US.UTF-8
    
    #!/usr/bin/perl 
    use DBI;
    my $dbName = 'hrdb';
    my $dbUser = 'nc';
    my $dbUserPass = 'xx';
    my $dbh = DBI->connect("dbi:Oracle:$dbName", $dbUser, $dbUserPass) or die "can't connect to database ";
    my $table_name= "$ARGV[0]"; 
    my $hostSql = qq{select COLUMN_NAME from dba_tab_columns where table_name='$table_name'};
    my $UNLOAD_SRC_DBCONN = DBI->connect("DBI:Oracle:".$dbName,$dbUser,$dbUserPass) or die("DB connect error!n");
    my $DW_DATA_DT ="";
    my $datafile="$table_name.TXT";
    use HTTP::Date qw(time2iso str2time time2iso time2isoz);
    
    
    my @lstRlst1;
    my @lstRlst;
    my ($COLUMN_NAME);
    my $selStmt = $dbh->prepare($hostSql);
    $selStmt->bind_columns(undef, $COLUMN_NAME);
    $selStmt->execute();
    while( $selStmt->fetch() ){
      print "$COLUMN_NAME
    ";
    push  (@lstRlst1 ,$COLUMN_NAME);
      }
      $selStmt->finish;
      $dbh->disconnect;
    my @lstRlst = reverse (@lstRlst1);
    ##########################################
    #=================全局变量区==========================#
    if ($#ARGV <0){
            print "请输入一个表名参数";
            exit(-1);
        }
    sub printlog
    {
      my ($LogInfo)= @_;
      my $CurrTime = time2iso(time());                   # 当前时间
      if(!defined($LogInfo) ){$LogInfo="";}
      my $StrLog="【${CurrTime}】 	 ${LogInfo} 
    "; 
      
      print $StrLog;
      #print LOGFILE $StrLog;
      }
      
    my $exportOracleSql="SELECT ";  #数据导出的sql
    for (my $m=0;$m<@lstRlst + 0 ;$m++){
     if  ($m != @lstRlst + 0 - 1){
      $exportOracleSql = "$exportOracleSql trim($lstRlst[$m])".", "
    }
    else{
    $exportOracleSql = "$exportOracleSql trim($lstRlst[$m])"}
    print "$exportOracleSql
    ";
    }
    my $exportOracleSql="$exportOracleSql from $dbUser.$table_name";
    
    sub Exportdata{
    	    
    	    printlog "开始导出数据!";
    	    my $exportsql=$exportOracleSql;
    	    if($exportsql eq "error"){
    	    	return -1;
    	    	}
    	    my $format_sql="alter session set nls_date_format='yyyy-mm-dd'";
    	    my $stmt=$UNLOAD_SRC_DBCONN->prepare($format_sql);
    	    unless ($stmt){
    			printlog "
    执行prepare SQL语句出错:
    ";
    			printlog $DBI::errstr; 
    			return -1;
    			}
    			$stmt->execute;
    			if ($UNLOAD_SRC_DBCONN->err) {
    			printlog "
    执行SQL语句出错:
    "; 
    			printlog $DBI::errstr;
    			return -1;
    			}
    	     $stmt=$UNLOAD_SRC_DBCONN->prepare($exportsql);
    	    unless ($stmt){
    			printlog "
    执行prepare SQL语句出错:
    ";
    			printlog $DBI::errstr;
    			return -1;
    		}
    	       $stmt->execute;
    		if ($UNLOAD_SRC_DBCONN->err) {
    			printlog "
    执行SQL语句出错:
    "; 
    			printlog $DBI::errstr;
    			
    			return -1;
    		}
    	     my $row=0;
    	     my $size=0;
    	     my $curtime;
    
    	     
    	     
    	     my $writeflagsql;
    	     my $tmpstr="";
    	     $row=0;
    	     my $m=0;              
     open(DATAFILE,">", $datafile) || die (print "Open DATA file failed!!!
    ");
    	     while(my $Rows = $stmt->fetchrow_arrayref){
    	     	$m=0;
    	     	$tmpstr="";
    	     	foreach(@$Rows){
    	     		$tmpstr=$tmpstr.$Rows->[$m]."|";
    	     		$m++;
    	     	}
    	     	print DATAFILE $tmpstr.$DW_DATA_DT."
    ";
    	     	$row++;
    	     	if(($row%10000) == 0){
    	     		printlog "已导出数据$row条!";
    	     	}	     	     	
    	    }
         	
            	$stmt->finish;
            #	print FLAGFILE $datafile,"
    ";
            #	print FLAGFILE $row,"
    ";
            	close(DATAFILE);
            #  close(FLAGFILE);
    
            	$curtime=time2iso(time());
              printlog "数据已成功导出!";
              printlog "一共导出数据${row}条";
                    
              return 1;	
    	
    	}
    Exportdata

  • 相关阅读:
    算法之--回溯法-迷宫问题【python实现】
    awk积累
    mysql自动化安装脚本(二进制安装)
    ${FUNCNAME[@]}和$LINENO使用
    shell脚本配置ssh免密登陆
    /etc/passwd和/etc/group文件详解
    Bagging与随机森林算法原理小结
    js之如何获取css样式
    Jetty源码学习-编译Jetty源码二三事
    maven安装和与IDE集成
  • 原文地址:https://www.cnblogs.com/zhaoyangjian724/p/6198983.html
Copyright © 2020-2023  润新知