如何在sql server的一个表中使用两个标识或自动增量列? [英] How to use two identity or auto increment column in one table in sql server?
本文介绍了如何在sql server的一个表中使用两个标识或自动增量列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用两个身份 或自动增量列 在一个表 在 sql server中?
i我为医院patientOpd创建一个表
CREATE TABLE [dbo]。[PatientOpd](
[Id] [ int ] NOT NULL ,
[OpdNo ] [ int ] IDENTITY ( 1 , 1 ) NOT NULL ,
[CreatedOn] [ datetime ] NOT NULL ,
[名称] [ nvarchar ]( 50 ) NULL ,
)
i想要 那个id < span class =code-keyword>和 OpdNo应该是自动递增值
as Opd no将每天重置和
Id将连续递增...
解决方案
你不能拥有SqlServer中有多个标识列。
如果您有SqlServer2012或更高版本,可以使用其中一列的序列。
否则你将不得不玩一个插入触发器,增加其中一列,但它是灾难恕我直言的秘诀。
CREATE TABLE dbo.Demo(ID INT IDENTITY PRIMARY KEY ,
IDwithChar AS ' C' + RIGHT (' 000000' + CAST(ID AS VARCHAR ( 10 )), 6 )PERSISTED
)
插入 进入演示默认 值跨度>
How to use two identity or auto increment column in one table in sql server??
i am creating a table for Hospital patientOpd
CREATE TABLE [dbo].[PatientOpd](
[Id] [int] NOT NULL,
[OpdNo] [int] IDENTITY(1,1) NOT NULL,
[CreatedOn] [datetime] NOT NULL,
[Name] [nvarchar](50) NULL,
)
i want to that id and OpdNo should be auto increment value
as Opd no will be reset daily and
Id will be increment continuously...
解决方案
You can not have more than one identity column in SqlServer.
If you have SqlServer2012 or later you can use a Sequence for one of the columns.
Otherwise you'll have to play around with an insert trigger that increments one of the columns, but it's a recipe for disaster IMHO.
CREATE TABLE dbo.Demo(ID INT IDENTITY PRIMARY KEY, IDwithChar AS 'C' + RIGHT('000000' + CAST(ID AS VARCHAR(10)), 6) PERSISTED )
insert into Demo default values
这篇关于如何在sql server的一个表中使用两个标识或自动增量列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文