Another approach would be to code it as a bulk operation, performing an insert, catching exceptions, and updating based on those exceptions.

There's an example that you could use as a starting point here