How to specify sql server identity

770 Views Asked by At

Does anyone know how to specify the format for an identity column in sql server. My identity column is also a primary key.

THe expected format is like:

ID_cli -------- Name

1000 1 ------------- aaa

1000 2 ------------- bbb

1000 3 ------------- ccc

1000 12346 ------- ddd

The identity column is ID_cli and the 1000 needs to be constant

2

There are 2 best solutions below

3
On

Here is the syntax:

create table tablename (ID_cli int identity(100,1),
AdditionalId as '0001'+Cast(ID as Varchar(10)))

Here Additional Id column will have ID values always prefixed with 0001.

IDENTITY [ (seed , increment) ]

seed:Is the value that is used for the very first row loaded into the table.

increment: Is the incremental value that is added to the identity value of the previous row that was loaded.

0
On

You cannot use just an identity for your purposes. You will need to do a custom solution. I can suggest that you have a column with the identity and a computed column that appends the "1000 " to is.