Results 1 to 5 of 5

Thread: [RESOLVED] DB (Access or SQL Server) - Using pure SQL find first available Key in Table

Threaded View

  1. #1

    Thread Starter
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Resolved [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
  •  



Click Here to Expand Forum to Full Width