Solved

Auto Increment field in SQL server

Posted on 2014-03-17
3
1,319 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

707 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

17 Experts available now in Live!

Get 1:1 Help Now