- 论坛徽章:
- 0
|
#!/usr/bin/perl
#############################################################################
use strict;
use DBI;
use DBD::mysql;
use DBD::DB2;
use DBD::DB2::Constants;
####################
#建立连接
####################
my ($dsn,$dbh1,$dbh2);
my ($sth1, $sth2, $table1, $table2);
my $count;
########源机的
my $host = '10.28.24.22';
my $dbn = "EMDB";
my $user = "db2inst1";
my $pd = "db2inst1";
##DB2
$dsn = "dbi:DB2:$dbn";
while(!($dbh1=DBI->connect($dsn, $user, $pd))){};
$dbh1->{LongReadLen} = 1024 * 1024;
########目的机的(到2009-02-27日)
$host = '222.34.43.2';
$dbn = "ycdata";
$user = "zheng";
$pd = "44444";
##MySQL5
$dsn = "DBI:mysql:$dbn:$host";
while(!($dbh2=DBI->connect($dsn, $user, $pd))){};
$dbh2->do("SET NAMES gbk"); #能解决表类型为utf8的汉字问题(其实是标识SQL字集符集)
###################################################################################
###################################################################################
## #库存表(业务)
$table1 = "EM.T_WH_BUSINESSINVENTORY";
$table2 = "t_em_businessinventory";
##找出 #1
$sth1 = $dbh1->prepare("SELECT * FROM $table1");
$sth1->execute || die "$DBI::errstr\n";
##删除 #2
$sth2 = $dbh2->prepare("TRUNCATE TABLE $table2");
$sth2->execute || die "$DBI::errstr\n";
##
eval {
$count=0;
while (my $refhash = $sth1->fetchrow_hashref()) {
if ($@) {
next;
}
$sth2 = $dbh2->prepare("INSERT INTO $table2 (ID,
WHID,
PRODUCTID,
PRODUCTCODE,
QUANT,
AMOUNT,
QUANTALLOCATED,
TAGRESERVED,
BUSINESSMONTH)
VALUES (?,?,?,?,?,
?,?,?,?)")
|| die $dbh2->errstr;
########
$sth2->bind_param(1, $refhash->{'ID'} ,4);
$sth2->bind_param(2, $refhash->{'WHID'} ,4);
$sth2->bind_param(3, $refhash->{'PRODUCTID'} ,4);
$sth2->bind_param(4, $refhash->{'PRODUCTCODE'} ,12);
$sth2->bind_param(5, $refhash->{'QUANT'} ,4);
$sth2->bind_param(6, $refhash->{'AMOUNT'} ,3);
$sth2->bind_param(7, $refhash->{'QUANTALLOCATED'} ,4);
$sth2->bind_param(8, $refhash->{'TAGRESERVED'} ,4);
$sth2->bind_param(9, $refhash->{'BUSINESSMONTH'} ,9);
if($sth2->execute) {
$sth2->finish();
$count++;
} else {
print "表:$table2 插入KEY=$refhash->{'ID'}的行时失败\n";
exit;
} #end if
} #end while
};
print "表:$table2 已完成,成功导入:$count 条记录\n";
$sth1->finish();
###################################################################################
###################################################################################
$dbh1->disconnect();
$dbh2->disconnect();
print "全部完成\n";
sleep (5);
exit;
这样是可以导,但就是速度太慢,每条记录都要insert一下。有没有批量insert的优化方案? |
|