Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Auto Increment field in SQL server

Posted on 2014-03-17
3
Medium Priority
?
1,571 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

824 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