本文介绍了SQLServer IDENTITY列的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在SQLServer中创建IDENTITY列,并在该列中添加文字?

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

示例:



ABCD-987065
ABCD-987066
ABCD-987067

推荐答案

除了其他答案,您还可以创建计算表上的列以提供您所要的内容.

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)) 
)

或者:

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...)

这篇关于SQLServer IDENTITY列的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 12:34