Suppose I am doing an insert or an update on a table. So in the BEGIN CATCH/END CATCH I can define a variable to ERROR_MESSAGE() and get back my error message:
Cannot insert the value NULL into column ‘columnname’, table ‘Table’; column does not allow nulls. INSERT fails.
Is there any way I could return say the primary key of the offending record or anything to identify which row actually failed? I rollback the transaction on failure so it’s not like I can look at the ‘last’ record to see the next one which has the problem.
This isn’t possible AFAIK due to the fact you’re not truly working on one record at a time.
Of course for inserts there are no primary keys yet assigned. If you really need that granular level of detail the best advice I can offer is to insert/update one row at a time so you know for sure which one threw the error.