|
-
Mar 25th, 2009, 05:41 PM
#1
[RESOLVED] DB (Access or SQL Server) - Using pure SQL find first available Key in Table
How to get the first Key available in a given Table, assumming some entries has been deleted, there are gaps, i need an SQL Query to get the first available, examples:
1, 2, 3, 6, 7, 8, 10, 14, 15
(it should return 4)
6, 7, 8, 10, 14, 15
(it should return 1)
I did this one, but there is a problem, this query returns the Key available after the first gap, not before..
Code:
SELECT MIN(a.CODE-1)
FROM Table AS a
LEFT JOIN Table AS b ON a.CODE - 1 = b.CODE
WHERE b.CODE IS NULL AND a.CODE <> 1;
Last edited by jcis; Mar 25th, 2009 at 05:48 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|