Click here to Skip to main content
13,449,155 members (51,213 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I want to generate ID number automatically using Sql server 2008,

Ex-sample Id Number

This my table

         (UserID INT IDENTITY(1,1) NOT NULL,//Only genarate Auto NUMBER
          UserName VARCHAR(50) NOT NULL,
          Password VARCHAR(50) NOT NULL)
Posted 15-Jan-13 18:43pm
Updated 15-Jan-13 20:56pm
Vani Kulkarni 16-Jan-13 1:21am
If you are setting Identity, it will generate values serially, i.e., 1,2, on. It will not generate like sample you have given.
Tharaka MTR 16-Jan-13 2:53am
yes, I agreed with Vani's comment. What is the problem here? or could you please give us more information.
Tharaka MTR 16-Jan-13 2:55am
oh.. I think you want to create auto number like A100, B100? is it?
what is the starting number? what is the starting number and ending number for each series.
give us more sample series ..
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You better format your output in the presentation layer.
You can format on selection with T-SQL built-in tools:
SELECT 'A' + RIGHT('000'+ CONVERT(VARCHAR, UserID), 3), UserName, [Password] from Users

But you can also use CLR integrated function also, thus you can make complex formatting using c# for example, having the whole framework behind you. See:[^]

I am not sure I got your idea. Especially how the sequence will work. You should think about how you will get the one id from the previous, and the next from the current one. More precisely you need a function that maps 1,2,3... to your id format. If you have this as an algorithm, you can decide on what level you want to implement it. Be aware, that you don't need an identity at all, you will probably need only a primary key, and that one has to be unique only.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


datatype-->Datatypes used in the transact-sql except for bit and decimal.
seed-->Is the value that is used for the very first row loaded into the table.
increment-->Is the incremental value that is added to the identity value of the previous row that was loaded.

Here, have a look at this links

Now you easily understood the basis of of using Identity. And If you want to use A100,B100, on, you have to create your own logic.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

search for (Custom Auto-Generated Sequences with SQL Server on google) ....i think u want that...and same question is hear...hope it will help you[^][^]

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180318.3 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100