Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi all,

I basically want to setup a identity column. An id that will be the primary key for all my sub records.

There a many methods my current way is to define a int column and on insert set the id to max(id)+1 now thats working fine at the moment. Is there a chance that with a high volumn of transactions inserting I could get into trouble with 2 transactions hitting the server at the same time?

thanks
Ash
Posted
Updated 29-May-11 6:25am
v2

 
Share this answer
 
Comments
thatraja 29-May-11 13:53pm    
Good links
Monjurul Habib 29-May-11 14:13pm    
thank you.
Ashman786 30-May-11 5:23am    
Thanks going through it now... :)
Ashman786 30-May-11 9:49am    
I've decided to use a combination of the answers. I'm going to use the NEWSEQUENTIALID() method described in http://www.codeproject.com/KB/database/AgileWareNewGuid.aspx?display=Print0

Thanks again for your help!
Monjurul Habib 30-May-11 10:45am    
pleasure.
Working with Max(id) + 1 is not an ideal solution. You should work by setting the column as Identity. Here is how you can do this IDENTITY (Property) (Transact-SQL)[^]

[EDIT]

You can return the Identity value in three different ways.
1. IDENT_CURRENT[^] T-SQL
2. @@IDENTITY[^] T-SQL
3. SCOPE_IDENTITY()[^] T-SQL

Each one of them have their own way of returning the identity value.
E.g If you have a Table called Movie with columns MovieID, Name , datatype int and nvarchar(30) respectively and MovieID is set as an Identity column, then
SQL
INSERT INTO Movie VALUES('BatMan')
SELECT SCOPE_IDENTITY() 

T-SQL will yield the last inserted movie,"BatMan" identity value.

Things to note regarding an Identity T-SQL,
1. A table will contain only one Identity column.
2. An Identity column can only be applied to tinyint, smallint, int, bigint, decimal(p,0), or numeric(p,0) datatypes.
 
Share this answer
 
v2
Comments
RaviRanjanKr 29-May-11 12:43pm    
Good Suggestion, My 5 :)
Wonde Tadesse 29-May-11 12:45pm    
Thanks
thatraja 29-May-11 13:52pm    
Agree, Good solution.
Wonde Tadesse 29-May-11 13:53pm    
Thanks.
Prasanta_Prince 29-May-11 21:15pm    
Right one. my 5.

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