SQL 服务器中的字母数字序列

Alpha-numeric sequence in SQL Server

我需要在 SQL Server 2008 中生成一个 3 个字符的字母数字序列,如下所示:

001,
002,
...,
999,
A01,
A02,
...,
A99,
B01,
B02,
...,
Z99

序列中的下一项将从存储过程生成并存储在 NCHAR(3) table 列中。

要获得下一个序列,您可以添加 Id 喜欢

WITH seq AS
(
    SELECT 
        ROW_NUMBER() OVER (ORDER BY x.alpha + y.number + z.number) AS Id,
        CONVERT(nchar(3), x.alpha + y.number + z.number) AS Result
    FROM 
        (
            VALUES 
            ('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9'),
            ('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H'), ('I'), ('J'), 
            ('K'), ('L'), ('M'), ('N'), ('O'), ('P'), ('Q'), ('R'), ('S'), ('T'), 
            ('U'), ('V'), ('W'), ('X'), ('Y'), ('Z')
        ) x(alpha),
        (
            VALUES 
            ('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9')
        ) y(number),
        (
            VALUES 
            ('0'), ('1'), ('2'), ('3'), ('4'), ('5'), ('6'), ('7'), ('8'), ('9')
        ) z(number)
    WHERE
       NOT (NOT x.alpha BETWEEN '1' AND '9' AND y.number = '0' AND z.number = '0')
)
-- Uncomment to see all results
--SELECT * FROM seq

SELECT Result FROM seq WHERE Id = (SELECT Id + 1 FROM seq WHERE Result = 'Z01')

结果

Z02

完整计数 = 3573 = 999 + (26 * 99)

您建议的存储过程的另一种方法是按照@user2864740 的建议使用 CROSS JOIN,并有一个名为 @pLastUsed 的参数。然后 SP returns 序列中的下一个。注意这将适用于所有版本的 SQL 服务器。

CREATE PROCEDURE NextID 
    @pLastUsed CHAR(3) 
AS
/* Usage
EXEC NextID 'J64'

*/
BEGIN
    SET NOCOUNT ON;
  DECLARE @T1 TABLE (Col CHAR(1))
  DECLARE @T2 TABLE (Col CHAR(1))
  DECLARE @T3 TABLE (Col CHAR(1))
  INSERT @T1 ( Col ) SELECT '0' UNION ALL SELECT '1' UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' 
                                UNION ALL SELECT '5' UNION ALL SELECT '6' UNION ALL SELECT '7' UNION ALL SELECT '8' 
                                UNION ALL SELECT '9' UNION ALL SELECT 'A' UNION ALL SELECT 'B' UNION ALL SELECT 'C'
                                UNION ALL SELECT 'D' UNION ALL SELECT 'E' UNION ALL SELECT 'F' UNION ALL SELECT 'G'
                                UNION ALL SELECT 'H' UNION ALL SELECT 'I' UNION ALL SELECT 'J' UNION ALL SELECT 'K'
                                UNION ALL SELECT 'L' UNION ALL SELECT 'M' UNION ALL SELECT 'N' UNION ALL SELECT 'O'
                                UNION ALL SELECT 'P' UNION ALL SELECT 'Q' UNION ALL SELECT 'R' UNION ALL SELECT 'S'
                                UNION ALL SELECT 'T' UNION ALL SELECT 'U' UNION ALL SELECT 'V' UNION ALL SELECT 'W'
                                UNION ALL SELECT 'X' UNION ALL SELECT 'Y' UNION ALL SELECT 'Z'
  INSERT @T2 ( Col ) SELECT '0' UNION ALL SELECT '1' UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' 
                                UNION ALL SELECT '5' UNION ALL SELECT '6' UNION ALL SELECT '7' UNION ALL SELECT '8' 
                                UNION ALL SELECT '9'
  INSERT @T3 ( Col ) SELECT '0' UNION ALL SELECT '1' UNION ALL SELECT '2' UNION ALL SELECT '3' UNION ALL SELECT '4' 
                                UNION ALL SELECT '5' UNION ALL SELECT '6' UNION ALL SELECT '7' UNION ALL SELECT '8' 
                                UNION ALL SELECT '9'
  SELECT TOP 1
    t1.Col + t2.Col + t3.Col
  FROM
    @T1 t1
    CROSS JOIN @T2 AS t2
    CROSS JOIN @T3 AS t3
  WHERE
    t1.Col + t2.Col + t3.Col > @pLastUsed
  ORDER BY
    t1.Col + t2.Col + t3.Col
END
GO

分享一个在SQL服务器中生成字母数字序列号的例子。

For more reference, You can visit this article.

创建样本table:

CREATE TABLE dbo.tbl_TestVarcharSequence
(
    VarcharID VARCHAR(10)
    ,Name VARCHAR(255)
    ,CONSTRAINT pk_tbl_TestVarcharSequence_VarcharID PRIMARY KEY(VarcharID)
)
GO

创建样本序列对象:

CREATE SEQUENCE dbo.seq_TestVarcharSequenceNumber AS 
INT START WITH 1
INCREMENT BY 1;
GO

使用 FORMAT 函数创建序列约束:

ALTER TABLE dbo.tbl_TestVarcharSequence
ADD CONSTRAINT seq_tbl_TestVarcharSequence_VarcharID DEFAULT 
FORMAT((NEXT VALUE FOR dbo.seq_TestVarcharSequenceNumber),'ABC00#') 
FOR VarcharID;
GO

插入几条样本记录:

INSERT INTO dbo.tbl_TestVarcharSequence (Name) 
VALUES ('Anvesh'),('Alex'),('Roy'),('Bony')
GO

结果:

SELECT * FROM dbo.tbl_TestVarcharSequence

ABC001     Anvesh
ABC002     Alex
ABC003     Roy
ABC004     Bony