When a unique index is being violated the message that is being raised looks like:
"Cannot insert duplicate key row in object 'dbo.OSUSR_V98_ROOM' with unique index 'OSIDX_OSUSR_V98_ROOM_10ROOMNUMBER'. The duplicate key value is (101). The statement has been terminated."
What is the best practice to handle this?
I could for example check the error message for "%unique index%" or'%duplicate key%', and then raise my message: "The Room with this number already exists". But this only works when there is just one unique index. Therefore I would rather show the message that corresponds to the specific unique index that is being violated.
Unfortunately the name of the Unique Index is not available in the error-message, so I am not able to make an error handling process to determine the message for the specific unique index.
Wouldn't it be great if the name of the unique index was part of the error message?