SQLServer IDENTITY Column with text

后端 未结 8 788
南旧
南旧 2020-11-30 10:48

How would I create an IDENTITY column in SQLServer with text in the column?

Example:


ABCD-987065
ABCD-987066
ABCD-987067

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 11:06

    In addition to the other answers, you could create a computed column on the table to provide what you are asking for.

    CREATE TABLE dbo.MyTable
    (
        Id int NOT NULL PRIMARY KEY,
        CombinedId AS 'ABCD-' + CAST(Id as varchar(16)) 
    )
    

    Or:

    CREATE TABLE dbo.MyTable
    (
        Id int NOT NULL PRIMARY KEY,
        PrefixField varchar(16),
        CombinedId AS PrefixField + CAST(Id as varchar(16)) 
    )
    

    (Your question doesn't say whether the prefix is intended to be fixed or not...)

提交回复
热议问题