Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Auto Increment field in SQL server

Posted on 2014-03-17
3
Medium Priority
?
1,531 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Assisted Solution

by:COACHMAN99
COACHMAN99 earned 664 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 40

Assisted Solution

by:lcohan
lcohan earned 668 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 668 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Viewers will learn how the fundamental information of how to create a table.
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…

715 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