# Reset number of tries $trycount=0; { # Increase number of tries $trycount++; # Make sure we don't try forever last if ($trycount > 10) # Put last id + 1 in $id SELECT MAX(id)+1 FROM table; # Try to insert new row and check for errors INSERT INTO table (id,col1,coln) VALUES ($id,$col1,$coln); # Try again redo if (error eq "Unable to insert due to duplicate values"); } # Handle too many tries die "Too many tries" if ($trycount > 10);