如何在 MS Access 中找到 table 中的第一个可用值

How to find the first available value in a table in MS Access

在 SQL Server 2008 中,我使用以下存储过程在 table 中找到第一个可用插槽。

If Exists ( Select *  From Methods  Where MethodSerno =1 )  
   Select @SlotCode = Min(MethodSerno) + 1  
   From Methods  
   Where MethodSerno + 1 Not In ( Select MethodSerno  From Methods )

MS Access 中是否有等效的方法?

提前致谢...

对于包含行

的名为 [Methods] 的 table
MethodSerno
-----------
          1
          2
          4
          5
          8
         10

我们可以从一个查询开始,找到每个 有直接后继 (n+1)

的值
SELECT t1.MethodSerno
FROM 
    Methods t1 
    LEFT JOIN
    Methods t2
        ON t1.MethodSerno + 1 = t2.MethodSerno
WHERE t2.MethodSerno IS NULL

回归

MethodSerno
-----------
          2
          5
          8
         10

"first available" 值将只是 (the smallest of those values)+1

SELECT MIN(t1.MethodSerno) + 1 AS NextSerno
FROM 
    Methods t1 
    LEFT JOIN
    Methods t2
        ON t1.MethodSerno + 1 = t2.MethodSerno
WHERE t2.MethodSerno IS NULL

回归

NextSerno
---------
        3