connectDB(); my $insert = "Insert Into table (id,name) Values (?,?)"; my $update = "Update table set name = ? Where id=?"; my $sth_insert = $dbh->prepare($insert) or die $dbh->errstr; my $sth_update = $dbh->prepare($update) or die $dbh->errstr; for my $data (@$data_ref){ if (! $sth_insert->execute( $$data{'id'}, $$data{'name'} ) ){ print 'Insert error: '.$sth_insert->errstr if $debug; print "Doing update\n"; $sth_update->execute( $$data{'name'}, $$data{'id'} ) or die("Insert and Update failed, update: ".$sth_update->errstr ); } } $sth_insert->finish(); $sth_update->finish(); disconnectDB();