这是我认为可以为您提供 Next 价值的代码。我创建了 3 个函数。该表只是我使用您的 alpha id 对 table.column 的模拟(我使用了 MyTable.AlphaID)。我假设它正如您所暗示的那样,并且有一个由五个字符的大写字母字符串 (AlphaID) 组成的连续块:
IF OBJECT_ID('dbo.MyTable','U') IS NOT NULL
DROP TABLE dbo.MyTable
GO
CREATE TABLE dbo.MyTable (AlphaID char(5) PRIMARY KEY)
GO
-- Play with different population scenarios for testing
INSERT dbo.MyTable VALUES ('ZZZZY')
INSERT dbo.MyTable VALUES ('ZZZZZ')
INSERT dbo.MyTable VALUES ('AAAAA')
INSERT dbo.MyTable VALUES ('AAAAB')
GO
IF OBJECT_ID('dbo.ConvertAlphaIDToInt','FN') IS NOT NULL
DROP FUNCTION dbo.ConvertAlphaIDToInt
GO
CREATE FUNCTION dbo.ConvertAlphaIDToInt (@AlphaID char(5))
RETURNS int
AS
BEGIN
RETURN 1+ ASCII(SUBSTRING(@AlphaID,5,1))-65
+ ((ASCII(SUBSTRING(@AlphaID,4,1))-65) * 26)
+ ((ASCII(SUBSTRING(@AlphaID,3,1))-65) * POWER(26,2))
+ ((ASCII(SUBSTRING(@AlphaID,2,1))-65) * POWER(26,3))
+ ((ASCII(SUBSTRING(@AlphaID,1,1))-65) * POWER(26,4))
END
GO
IF OBJECT_ID('dbo.ConvertIntToAlphaID','FN') IS NOT NULL
DROP FUNCTION dbo.ConvertIntToAlphaID
GO
CREATE FUNCTION dbo.ConvertIntToAlphaID (@ID int)
RETURNS char(5)
AS
BEGIN
RETURN CHAR((@ID-1) / POWER(26,4) + 65)
+ CHAR ((@ID-1) % POWER(26,4) / POWER(26,3) + 65)
+ CHAR ((@ID-1) % POWER(26,3) / POWER(26,2) + 65)
+ CHAR ((@ID-1) % POWER(26,2) / 26 + 65)
+ CHAR ((@ID-1) % 26 + 65)
END
GO
IF OBJECT_ID('dbo.GetNextAlphaID','FN') IS NOT NULL
DROP FUNCTION dbo.GetNextAlphaID
GO
CREATE FUNCTION dbo.GetNextAlphaID ()
RETURNS char(5)
AS
BEGIN
DECLARE @MaxID char(5), @ReturnVal char(5)
SELECT @MaxID = MAX(AlphaID) FROM dbo.MyTable
IF @MaxID < 'ZZZZZ'
RETURN dbo.ConvertIntToAlphaID(dbo.ConvertAlphaIDToInt(@MaxID)+1)
IF @MaxID IS NULL
RETURN 'AAAAA'
SELECT @MaxID = MAX(AlphaID)
FROM dbo.MyTable
WHERE AlphaID < dbo.ConvertIntToAlphaID((SELECT COUNT(*) FROM dbo.MyTable))
IF @MaxID IS NULL
RETURN 'AAAAA'
RETURN dbo.ConvertIntToAlphaID(dbo.ConvertAlphaIDToInt(@MaxID)+1)
END
GO
SELECT * FROM dbo.MyTable ORDER BY dbo.ConvertAlphaIDToInt(AlphaID)
GO
SELECT dbo.GetNextAlphaID () AS 'NextAlphaID'
顺便说一句,如果您不想假设连续,您可以按照您的建议进行操作,并且(如果有“ZZZZZ”行)使用序列中的第一个间隙。用这个替换最后一个函数:
IF OBJECT_ID('dbo.GetNextAlphaID_2','FN') IS NOT NULL
DROP FUNCTION dbo.GetNextAlphaID_2
GO
CREATE FUNCTION dbo.GetNextAlphaID_2 ()
RETURNS char(5)
AS
BEGIN
DECLARE @MaxID char(5), @ReturnVal char(5)
SELECT @MaxID = MAX(AlphaID) FROM dbo.MyTable
IF @MaxID < 'ZZZZZ'
RETURN dbo.ConvertIntToAlphaID(dbo.ConvertAlphaIDToInt(@MaxID)+1)
IF @MaxID IS NULL
RETURN 'AAAAA'
SELECT TOP 1 @MaxID=M1.AlphaID
FROM dbo.Mytable M1
WHERE NOT EXISTS (SELECT 1 FROM dbo.MyTable M2
WHERE AlphaID = dbo.ConvertIntToAlphaID(dbo.ConvertAlphaIDToInt(M1.AlphaID) + 1 )
)
ORDER BY M1.AlphaID
IF @MaxID IS NULL
RETURN 'AAAAA'
RETURN dbo.ConvertIntToAlphaID(dbo.ConvertAlphaIDToInt(@MaxID)+1)
END
GO