#!C:\Perl64\bin@H_419_7@ #通过BCP备份和还原sqlSERVER指定的表@H_419_7@ use 5.014;
#加载用户和密码配置文件@H_419_7@ my $username ;@H_419_7@ my $passwd;
##获取用户名@H_419_7@ my $dir="c:";@H_419_7@ $dir=$dir."/";@H_419_7@ my $filename="config.txt";@H_419_7@ my $keysword="username:";@H_419_7@ my $count="50";@H_419_7@ my $begincount=index($keysword,":");@H_419_7@ my($file);@H_419_7@ open(fileH,$dir.$filename);@H_419_7@ while(my $test=<fileH>){@H_419_7@ if((index($test,$keysword))>=0){@H_419_7@ my $test1=substr($test,$begincount+1,$count);@H_419_7@ #print "$test1"; @H_419_7@ chomp($username=$test1);@H_419_7@ }@H_419_7@ }@H_419_7@ close(fileH);
@H_419_7@ ##获取密码@H_419_7@ my $dir="c:";@H_419_7@ $dir=$dir."/";@H_419_7@ my $filename="config.txt";@H_419_7@ my $keysword="password:";@H_419_7@ my $count="50";@H_419_7@ my $begincount=index($keysword,$keysword))>=0){@H_419_7@ my $test2=substr($test,$count);@H_419_7@ #print "$test2";@H_419_7@ chomp($passwd=$test2); @H_419_7@ }@H_419_7@ }@H_419_7@ close(fileH);@H_419_7@ print "-------------------------------------------------------------------\n";@H_419_7@ print "请首先检查用户和密码配置文件config.txt保存路径是否为$dir$filename\n";@H_419_7@ print "-------------------------------------------------------------------\n";
print "重要提示:备份表 *** 作请输入0;还原表 *** 作请输入1\n";@H_419_7@ print "请输入 *** 作代码:";@H_419_7@ chomp(my $inp=<STDIN>);@H_419_7@ print "你输入的 *** 作代码为$inp\n";
#判断输入为0即备份 *** 作@H_419_7@ if ($inp eq 0){@H_419_7@ print '服务器IP:(不输入默认为localhost)';@H_419_7@ chomp(my $a = <STDIN>);@H_419_7@ if ($a==''){@H_419_7@ $a='localhost';@H_419_7@ }else{@H_419_7@ print "你输入的服务器IP为:$a\n";@H_419_7@ }
@H_419_7@ print '用户名:';@H_419_7@ chomp(my $b = <STDIN>);@H_419_7@ print "你输入的用户名为:$b\n";@H_419_7@ if($b eq $username) {
print "用户通过!\n";
} else {@H_419_7@ print "对不起,用户名错误\n";@H_419_7@ exit;@H_419_7@ }
print "用户密码: ";@H_419_7@ chomp (my $readPsw = <STDIN>);@H_419_7@ if($readPsw eq $passwd) {
print "密码正确!\n";
} else {@H_419_7@ print "对不起,密码错误\n";@H_419_7@ exit;@H_419_7@ }
@H_419_7@ print "重要提示:备份表的表名格式为:test.dbo.t1\n";@H_419_7@ print '备份的表:';@H_419_7@ chomp(my $d = <STDIN>);@H_419_7@ print "你输入的备份的表:$d\n";
@H_419_7@ print '保存路径:';@H_419_7@ chomp(my $e = <STDIN>);@H_419_7@ print "你输入的保存路径:$e\n";
if (-e $e){@H_419_7@ print "$e已经存在!是否删除?(y删除;n不删除)\n";@H_419_7@ chomp(my $i=<STDIN>);@H_419_7@ given ($i)@H_419_7@ {@H_419_7@ when ("y") {@H_419_7@ unlink $e;@H_419_7@ print "开始备份表!\n";@H_419_7@ system (" bcp $d out $e -c -t ',' -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");@H_419_7@ }@H_419_7@ when ("n") @H_419_7@ {@H_419_7@ print "-----------备份已经终止-----------\n";@H_419_7@ print "指定文件存在,请重新输入文件名!\n";@H_419_7@ }@H_419_7@ default {print "只能输入y或n!\n";}@H_419_7@ }@H_419_7@ }@H_419_7@ else{@H_419_7@ {@H_419_7@ system (" bcp $d out $e -c -t ',' -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");@H_419_7@ }@H_419_7@ }@H_419_7@ }
#判断输入为1还原 *** 作@H_419_7@ elsif($inp eq 1){@H_419_7@ print '服务器IP:(不输入默认为localhost)';@H_419_7@ chomp(my $a = <STDIN>);@H_419_7@ if ($a==''){@H_419_7@ $a='localhost';@H_419_7@ }else{@H_419_7@ print "你输入的服务器IP为:$a\n";@H_419_7@ }@H_419_7@ print '用户名:';@H_419_7@ chomp(my $b = <STDIN>);@H_419_7@ print "你输入的用户名为:$b\n";@H_419_7@ if($b eq $username) {
print "用户通过!\n";
} else {@H_419_7@ print "对不起,密码错误\n";@H_419_7@ exit;@H_419_7@ }
print '需要还原的表:';@H_419_7@ chomp(my $d = <STDIN>);@H_419_7@ print "你输入的备份的表:$d\n";
@H_419_7@ print '备份保存路径:';@H_419_7@ chomp(my $e = <STDIN>);@H_419_7@ if (-e $e) {@H_419_7@ print "你输入的备份保存路径:$e\n";@H_419_7@ system (" bcp $d in $e -c -t ',' -m 1 -a 49152 -b 5000 -F 1 -S $a -U $b -P $readPsw ");@H_419_7@ }@H_419_7@ else {@H_419_7@ print "对不起,指定的备份文件$e不存在,请核实!\n";@H_419_7@ }@H_419_7@ }@H_419_7@ else{@H_419_7@ print "请参考重要提示!\n";@H_419_7@ }@H_419_7@ system 'pause';
----config.txt
username:sa@H_419_7@ password:passwd123!
----使用方法
1.将config.txt保存到c盘根目录
根据实际情况修改该文件内容
2.执行bcp.pl
总结以上是内存溢出为你收集整理的perl *** 作sqlserver实现BCP全部内容,希望文章能够帮你解决perl *** 作sqlserver实现BCP所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)