zoukankan      html  css  js  c++  java
  • perl操作sqlserver实现BCP

    #!C:Perl64in
    #由BCP备份和恢复SQLSERVER指定表
    use 5.014;

    #加载用户和password型材
    my $username ;
    my $passwd;

    ##得到username
    my $dir="c:";
    $dir=$dir."/";
    my $filename="config.txt";
    my $keysword="username:";
    my $count="50";
    my $begincount=index($keysword,":");
    my($file);
    open(FILEH,$dir.$filename);
     while(my $test=<FILEH>){
       if((index($test,$keysword))>=0){
     my $test1=substr($test,$begincount+1,$count);
        #print "$test1";
     chomp($username=$test1);
       }
     }
    close(FILEH);


    ##获取password
    my $dir="c:";
    $dir=$dir."/";
    my $filename="config.txt";
    my $keysword="password:";
    my $count="50";
    my $begincount=index($keysword,":");
    my($file);
    open(FILEH,$dir.$filename);
     while(my $test=<FILEH>){
       if((index($test,$keysword))>=0){
     my $test2=substr($test,$begincount+1,$count);
        #print "$test2";
     chomp($passwd=$test2);
       }
     }
    close(FILEH);
    print "------------------------------------------------------------------- ";
    print "请首先检查用户和password配置文件config.txt保存路径是否为$dir$filename ";
    print "------------------------------------------------------------------- ";

    print "重要提示:备份表操作请输入0;还原表操作请输入1 ";
    print "请输入操作代码:";
    chomp(my $inp=<STDIN>);
    print "你输入的操作代码为$inp ";

    #推断输入为0即备份操作
    if ($inp eq 0){
    print 'serverIP:(不输入默觉得localhost)';
    chomp(my $a = <STDIN>);
    if ($a==''){
    $a='localhost';
    }else{
    print "你输入的serverIP为:$a ";
    }


    print 'username:';
    chomp(my $b = <STDIN>);
    print "你输入的username为:$b ";
    if($b eq $username) {

      print "用户通过! ";

    } else {
      print "对不起,username错误 ";
      exit;
    }

    print "用户password: ";
    chomp (my $readPsw = <STDIN>);
    if($readPsw eq $passwd) {

      print "password正确! ";

    } else {
      print "对不起,password错误 ";
      exit;
    }


    print "重要提示:备份表的表名格式为:test.dbo.t1 ";
    print '备份的表:';
    chomp(my $d = <STDIN>);
    print "你输入的备份的表:$d ";


    print '保存路径:';
    chomp(my $e = <STDIN>);
    print "你输入的保存路径:$e ";

    if (-e $e){
    print "$e已经存在!是否删除?(y删除;n不删除) ";
    chomp(my $i=<STDIN>);
    given ($i)
    {
    when ("y") {
    unlink $e;
    print "開始备份表! ";
    system (" bcp $d out $e -c -t ',' -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");
    }
    when  ("n")
    {
    print "-----------备份已经终止----------- ";
    print "指定文件存在,请又一次输入文件名称! ";
    }
    default {print "仅仅能输入y或n! ";}
    }
    }
    else{
    {
    system (" bcp $d out $e -c -t ',' -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");
    }
    }
    }

    #推断输入为1还原操作
    elsif($inp eq 1){
    print 'serverIP:(不输入默觉得localhost)';
    chomp(my $a = <STDIN>);
    if ($a==''){
    $a='localhost';
    }else{
    print "你输入的serverIP为:$a ";
    }
    print 'username:';
    chomp(my $b = <STDIN>);
    print "你输入的username为:$b ";
    if($b eq $username) {

      print "用户通过! ";

    } else {
      print "对不起,username错误 ";
      exit;
    }

    print "用户password: ";
    chomp (my $readPsw = <STDIN>);
    if($readPsw eq $passwd) {

      print "password正确! ";

    } else {
      print "对不起,password错误 ";
      exit;
    }

    print '须要还原的表:';
    chomp(my $d = <STDIN>);
    print "你输入的备份的表:$d ";


    print '备份保存路径:';
    chomp(my $e = <STDIN>);
    if (-e $e) {
    print "你输入的备份保存路径:$e ";
    system (" bcp $d in $e -c -t ',' -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");
    }
    else {
    print "对不起,指定的备份文件$e不存在,请核实! ";
    }
    }
    else{
    print "请參考重要提示! ";
    }
    system 'pause';

    ----config.txt

    username:sa
    password:passwd123!

    ----用法

    1.将config.txt保存到c文件夹包装

    根据实际情况更改文件的内容

    2.运行bcp.pl


     

  • 相关阅读:
    线性代数回顾+深化(未完成版)
    HIT OS2020 Spring Lab2
    选择
    工业互联网
    leetcode-200 岛屿数量
    记网易面试题<二>
    记网易面试题《一》
    leetecode-14-最长公共子串-简单
    leetcode-1012 至少有1位重复的数字
    协程
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4600277.html
Copyright © 2011-2022 走看看