创建从最后一个索引增加的列 [英] Create column which increases from last index

查看:25
本文介绍了创建从最后一个索引增加的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 InvoiceID 生成一列.我想保持这个专栏的格式

I need to generate a column for InvoiceID. and I want to keep the formate of this column like this

INV0000001,
INV0000002,
.
.
.
.
INV0000010,
INV0000011,
.
. and so on. 

如您所见,此列随着最后一个索引的增加而增加.我怎样才能做到这一点.

As you can see this column is increasing with last index. How can i do this.

我使用的是 SQL Server 2012.

I'm using SQL Server 2012.

我已经搜索过,但找不到,如何增加这样的数字.

I have searched, but couldn't find, how to increase a number like this.

推荐答案

尝试使用 computed column MSDN

CREATE TABLE Yourtablename
(
    ID int IDENTITY (1,1) NOT NULL,
    InvoiceID AS 'INV'+ right('000000'+cast(ID as varchar(20)),7) PERSISTED
);

SQLFIDDLE 演示

有关为什么需要将计算列设为 persisted 的更多信息检查这里

For more info on why you need to make your computed column as persisted check here

这篇关于创建从最后一个索引增加的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆