0

如果我有一个硬编码值“04200001000000”并且想要自动增加最后一个数字。我在网上找不到有关此功能的任何信息。我正在使用 SQL Server 2014。

例子:

0420000100000001
0420000100000002
0420000100000003
0420000100000004
0420000100000005
0420000100000006
0420000100000007
0420000100000008
0420000100000009
0420000100000010
4

3 回答 3

0

尝试这个

DECLARE INT @CurrentPos = (SELECT MAX(CAST(TabEmpNo AS INT))
    FROM Table);

SET @CurrentPos = SELECT IIF(@CurrentPos IS NULL, 4200001000000, @CurrentPos);

WITH CTE
AS
(
    SELECT Col1,
        ROW_NUMBER() OVER (ORDER BY Col1) AS RN
    FROM TABLE
)
INSERT INTO TABLE
(
    Col1,
    TabEmpNo
)
SELECT Col1,
    CONCAT('0', CAST(RN + @CurrentPos AS VARCHAR(13))) AS TabEmpNo
FROM CTE
于 2017-06-25T01:13:50.710 回答
0

我们确实需要更多信息,但根据列的类型,这可能有效:

ALTER TABLE myTable ALTER COLUMN columnName IDENTITY (04200001000001, 1);

于 2017-06-22T19:24:06.610 回答
-1

假设您的员工“编号”实际上是字符数据类型(因为它包含前导零),您可能会使用 SEQUENCE。

例如

CREATE SEQUENCE dbo.EmpNumbers
AS BIGINT
START WITH 4200001000000
INCREMENT BY 1
MIN VALUE 4200001000000
MAX VALUE 9999999999999
CACHE 10
;

然后,您可以在添加新员工时使用它来生成新的员工编号,例如

SELECT 
     EmpName
   , '0' + CAST((NEXT VALUE FOR dbo.EmpNumbers) AS CHAR(13)) AS NewEmpNumber 
FROM NewEmployees
于 2017-06-22T20:37:57.823 回答