Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL
I want to generate ID number automatically using Sql server 2008,
 
Ex-sample Id Number
A100
B100
 
This my table
 
CREATE TABLE Users
         (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
Hasiya396
Edited 15-Jan-13 20:56pm
v2
Comments
Vani Kulkarni at 16-Jan-13 1:21am
   
If you are setting Identity, it will generate values serially, i.e., 1,2,3.....so on. It will not generate like sample you have given.
Tharaka MTR at 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 at 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
good
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: http://www.fotia.co.uk/fotia/Blog/2009/05/indispensable-sql-clr-functions.html[^]
 
[update]
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.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

IDENTITY(datatype,seed,increment)--Function
IDENTITY(seed,increment)--Property
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
IDENTITY(Function)[^]
IDENTITY(Property)[^]
 
Now you easily understood the basis of of using Identity. And If you want to use A100,B100,...so on, you have to create your own logic.
  Permalink  
Rate this: bad
good
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
http://forums.asp.net/t/1572015.aspx/1[^]
http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 504
1 Gihan Liyanage 298
2 RyanDev 250
3 Sergey Alexandrovich Kryukov 223
4 Richard Deeming 195
0 Sergey Alexandrovich Kryukov 8,906
1 OriginalGriff 7,771
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
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