SQL SERVER string auto increment column

       Sometimes we have special requirements for the auto-incrementing primary key, not only the numerical auto-increment, but also the effect of special characters + time + auto-increment value. Such requirements can be used when creating a new table. Add the primary key to set, the implementation method is as follows:

CREATE TABLE testtab
(
    ID INT IDENTITY,
    PRIMARYKEY AS 'T' + CONVERT(VARCHAR(100), GETDATE(), 112) + RIGHT(1000 + id, 4),
    TESTVALUE VARCHAR (50)
);

       Insert data:

INSERT INTO dbo.testtab
(
    TESTVALUE
)
VALUES ('Test 1' -- TESTVALUE - varchar(50)
       )

SELECT * FROM testtab

       The result is as follows:


       Above we have realized the problem of string auto-increment.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325808450&siteId=291194637