Click here to Skip to main content
15,921,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi i want to give auto increment for this character can any oen help me?
4MT08C5100 how to set for this in column datatype??what i have to selecte.i tried varchar but i cant make auto increment??

can any one suggest me??
Posted
Comments
ythisbug 6-Mar-12 5:28am    
i already created table now how to insert that query bro..pleas help me

1 solution

create an ID INT IDENTITY column and use that as your primary key (it's unique, narrow, static - perfect)
if you really need an ID with a letter or something, create a computed column based on that ID INT IDENTITY

Try something like this:
SQL
CREATE TABLE Demo(ID INT IDENTITY PRIMARY KEY,
                      IDwithChar AS '4MT08' + RIGHT('5100' + CAST(ID AS VARCHAR(10)), 6) PERSISTED)

This table would contain ID values from 1, 2, 3, 4........ and the IDwithChar would be something like 4MT085100, 4MT085101, ....., 4MT085199 and so forth.

With this, you have the best of both worlds:

-A proper, perfectly suited primary key (and clustering key) on your table, ideally suited to be referenced from other tables.

-Your character-based ID, properly defined, computed, always up to date
 
Share this answer
 
Comments
ythisbug 6-Mar-12 7:14am    
ALTER TABLE AllotmentTab
ADD Registration AS '4MT08' + RIGHT('5100' + CAST(ID AS VARCHAR(50)), 6)
PERSISTED)

hi i tried like this but m getting error near persisted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900