Id自动增量varchar不递增

时间:2016-05-02 10:51:17

标签: sql-server

我使用VARCHAR列作为主键。我想自动递增它(基数62,小写/大写,数字),但是,下面的代码失败(原因很明显):

CREATE TABLE TESTING
(
    ID VARCHAR(10),
    NAME VARCHAR(15),
    DESCP VARCHAR(50)
);

使用NVARCHAR自动递增ID创建测试插入新记录的过程:

CREATE PROCEDURE SP_INSERT 
     @NAME VARCHAR(MAX), @DESCP VARCHAR(MAX)
AS 
BEGIN
    /* Logic for getting new ID as per the NAME with PRE FIX */ 
    DECLARE @NEWID VARCHAR(5);
    DECLARE @PREFIX VARCHAR(10); 

    SET @PREFIX = UPPER(SUBSTRING('STR', 1, 3)) 

    SELECT @NEWID = (@PREFIX + replicate('0', 3 - len(CONVERT(VARCHAR, N.OID 1))) + CONVERT(VARCHAR, N.OID + 1)) 
    FROM 
        (SELECT 
             CASE 
                WHEN MAX(T.TID) IS NULL
                THEN 0 ELSE MAX(T.TID) 
             END AS OID 
         FROM 
             (SELECT SUBSTRING(ID, 1, 1) AS PRE_FIX,
                     SUBSTRING(ID, 2, LEN(ID)) AS TID 
              FROM Testing) AS T 
         WHERE 
             T.PRE_FIX = @PREFIX) AS N
   /* INSERT QUERY FOR NEW RECORD */
   INSERT INTO Testing VALUES (@NEWID, @NAME, @DESCP)

END

我尝试插入值:

SP_INSERT 'svce','YOUR MANAGEMENT DESCRIPTION';

这是第一次,再次尝试插入我得到的相同ID没有增加。

像:

ID     Name
------------
SVC001 SVCE
SVC001 Svce

4 个答案:

答案 0 :(得分:1)

您可以尝试使用类似的COMPUTED COLUMN

create table t
(
    Id int auto_increment,
    ComputedId as 'SVC' + convert(nvarchar(60), lpad(Id,3,'0')),
    Data nvarchar(60)
)

答案 1 :(得分:0)

我做了一个小小的脚本,

declare @t table (id varchar(10))
insert into  @t values ('SVC001'),('SVC20902')
Declare @NEWID int
select @NEWID=isnull(max(replace(id,'SVC',''))+1,1) from @t

select 'SVC'+ replicate('0',CASE WHEN 3-len(@NEWID)<=0 
THEN 0 ELSE 3-len(@NEWID) END)+cast(@NEWID as varchar(10))

答案 2 :(得分:0)

最后我得到了Auto_Increment ID的解决方案。我完成了以下代码解决方案。代码是:

alter PROCEDURE SP2_INSERT
      @NAME VARCHAR(MAX),
      @DESCP VARCHAR(MAX)
AS
BEGIN
      DECLARE @NEWID VARCHAR(10);
      DECLARE @PREFIX VARCHAR(MAX);
   SET @PREFIX = UPPER(SUBSTRING(@NAME,1, 3))

   SELECT @NEWID = (@PREFIX + replicate('0', 3 - len(CONVERT(VARCHAR,N.OID + 1))) + CONVERT(VARCHAR,N.OID + 1)) FROM (
   SELECT CASE WHEN MAX(T.TID) IS null then 0 else MAX(T.TID) end as OID FROM (
   SELECT SUBSTRING(EmployeeID, 1, 1) as PRE_FIX,SUBSTRING(EmployeeID, 2, LEN(EmployeeID)) as TID FROM EmployeeMasters
 ) AS T WHERE T.PRE_FIX = @PREFIX
) AS N
INSERT INTO Testing VALUES (@NEWID,@NAME,@DESCP)
end

答案 3 :(得分:-2)

试试这个,

DECLARE @table TABLE(ID VARCHAR(10),
 NAME VARCHAR(15),
 DESCP VARCHAR(50))

DECLARE @NEWID VARCHAR(5);
   DECLARE @PREFIX VARCHAR(10); 

SET @PREFIX = UPPER(SUBSTRING('STR', 1, 3)) 

 SELECT @NEWID=CAST(ISNULL(MAX(CAST(SUBSTRING(ID,4,LEN(ID)) AS INT)),0)+1 AS varchar) FROM @table
 INSERT INTO @table(DESCP,ID,NAME)
 SELECT 'zzzz',(@PREFIX + replicate('0', 3 - len(CONVERT(VARCHAR,@NEWID))))+@NEWID,'fsf'

 SELECT @NEWID=CAST(ISNULL(MAX(CAST(SUBSTRING(ID,4,LEN(ID)) AS INT)),0)+1 AS varchar) FROM @table
 INSERT INTO @table(DESCP,ID,NAME)
 SELECT 'zzzz',(@PREFIX + replicate('0', 3 - len(CONVERT(VARCHAR,@NEWID))))+@NEWID,'fsf'

 SELECT * FROM @table