Link to home
Start Free TrialLog in
Avatar of lankapala
lankapala

asked on

create auto number

Hi ,
I need to create auto number for customer number,according same with auto number filed
 see below code
Prefix='Cus'
CREATE TABLE dbo.Customers
(
Sequence   int  IDENTITY(1,1) NOT NULL
, Prefix     nchar(1) NOT NULL
, CustomerID AS Prefix + CAST(Sequence + 1000000 as nvarchar(8))
, CONSTRAINT UN_CustomersSequence UNIQUE (Sequence)
, CONSTRAINT PK_Customers PRIMARY KEY (CustomerID)

This is right way or any other proper way?
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why do you need to store de CustomerID? If you have Sequence and Prefix, every time you need the customer id you can perform the concatenation so you won't waste space in disk.
Anyway, how you're doing is correct. It's called Computed Column.
But im getting following error
Only UNIQUE or PRIMARY KEY constraints can be created on computed columns, while CHECK, FOREIGN KEY, and NOT NULL constraints require that computed columns be persisted.
Avatar of lankapala
lankapala

ASKER

??update?
What do you mean with "??update?"