Solved

Auto Increment field in SQL server

Posted on 2014-03-17
3
1,339 Views
Last Modified: 2014-03-18
Hi ,

I need to have a primary key field and a not null unique field  with auto increment by 1 .
with below format for field 1 . AB0000001 where AB will be prefix and is of 10 digit length.

and second filed CD00000001 with CD as prefix and 10 charater length. Both need to have auto increment by 1 every time user enters data.

What are the best solution for this ? I have SQL SERVER 2008 Express R2 installed
0
Comment
Question by:Vikash p
3 Comments
 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 166 total points
ID: 39934895
auto-incrementing strings is problematic. I would use the SQL identity field and in SQL I  would format the concatenation of 2 fields.
e.g. outputfield = 'CD' + format(ID,'0000000')
0
 
LVL 39

Assisted Solution

by:lcohan
lcohan earned 167 total points
ID: 39935086
Use 2 columns to form the PK where one would be a string data type (static "AB" char(2) for instance) AND the second column the IDENTITY auto increment by 1...

OR...even if not ideal you could use the GUID SQL data type with NEWID() to generate the unique, next value to be inserted.

SELECT NEWID();
0
 
LVL 5

Accepted Solution

by:
ggzfab earned 167 total points
ID: 39936684
I concur with COACHMAN99 to use a SQL identity field wit auto increment as that's the "normalized" way.
The "CD0000" string could be created using a function (e.g. "fncCode") when you need to print/show the field. That's separating the unique ID with the presentation and using such a function allows the change of the string for the entire application in one location! (Just imagine the 0's aren't sufficient...)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now