SQL Server 2005 - Find minimum unused value within a range

Posted by Geo Ego on Stack Overflow See other posts from Stack Overflow or by Geo Ego
Published on 2010-04-23T19:05:40Z Indexed on 2010/04/23 19:13 UTC
Read the original article Hit count: 222

Filed under:
|

I have a situation similar to the following question:

Insert Data Into SQL Table

Where my scenario differs is that I have a non-auto-incrementing primary key field that can have a range between 1000 and 1999. We only have about a hundred values in it thus far, but the maximum value has already been taken (1999), and there are gaps in the numbering sequence. Thus, I need to find a value that is between 1000-1999 and not taken. For instance, if my current values are, for example, 1000, 1001, 1003, and 1999, I would want the query to return 1002.

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2005