Preventing a Gap in Auto-Increment Numbers

Richard Waymire answers a reader’s question about triggering an uninterrupted series of numbers.

Richard Waymire

August 21, 2001

1 Min Read
ITPro Today logo in a gray background | ITPro Today

I selected an identity field as a primary key in a SQL Server table. When I delete the last record, then enter a new record, SQL Server creates a new number. However, when I insert a new record, I want SQL Server to check the maximum number in the auto- increment field, then assign the next consecutive number to prevent a gap. I think I can create a trigger that ensures an uninterrupted series of numbers. Is this solution appropriate?

You can create an INSTEAD OF trigger in SQL Server 2000 to close the number gaps. However, unless you have a compelling business case to use consecutive numbers, you should reevaluate this need. You're likely to suffer scalability and locking problems if you don't allow gaps in your incrementing counters.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like