Home:ALL Converter>SQL Server : Attempting to Insert a Duplicate Record Costs an Id

SQL Server : Attempting to Insert a Duplicate Record Costs an Id

Ask Time:2012-12-08T00:44:04         Author:Mickael Caruso

Json Formatter

I have the following table set up:

  • Id int pk, unique not null
  • Name varchar(50) not null
  • Other columns not relevant to this issue

With an index set up on Name to be unique and non-clustered.

The setup does EXACTLY what I want - that is, only insert new rows whose Name doesn't already exist in the table, and throw an error if the new row is a duplicate Name.

I might be nit-picky about it, but every attempt to add a duplicate will cause SQL Server to skip the next Id that would have been assigned, had the new row been a non-duplicate Name.

Is there a way to prevent this with some setting, without the need to query for existence first before deciding to insert or deny?

Author:Mickael Caruso,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/13767322/sql-server-attempting-to-insert-a-duplicate-record-costs-an-id
yy