Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to generate ID number automatically using Sql server 2008,

Ex-sample Id Number
A100
B100

This my table

SQL
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
Updated 15-Jan-13 20:56pm
v2
Comments
Vani Kulkarni 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 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 ..

You better format your output in the presentation layer.
You can format on selection with T-SQL built-in tools:
SQL
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.
 
Share this answer
 
v3
SQL
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.
 
Share this answer
 
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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900