while(@row = $sth->fetchrow_array) { ++$duplicate; } #### my $dbh = DBI->connect($dsn, $usr, $pwd, {RaiseError => 1}); my $sth = $dbh->prepare("insert into Customers(Names, ...) values(?, ...)"); eval { $sth->execute($new_name, ...); }; if($@) { # Got an error! # The insert failed - probably due to the duplicate key constraint # You can check the $@ variable which holds the error # string that caused the insert to fail to see what # error happened. # Handle this the same way that you would handle the # case where your checkDuplicateName() returns true }