Best way to generate userid(integer value), should i use identity column or something else? Please advise

I have a login table which generates users and generates an id for the user. I am not sure whether to use IDENTITY_Column(autonumber) so that sql server generates a unique id by itself or use something else.
Some people advised me using identity specification lock table and causes performance issues.

At present i am using the following:
SET NOCOUNT ON;
	DECLARE @intErrorCode INT
	BEGIN TRANSACTION
		SET @TID=(select (counterfrom + currentcounter) from e_counter 
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
		insert into e_Login
		(id,Username,Password) values
		(@TID, 'U-','U-')
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
		UPDATE e_counter set currentcounter=(currentcounter + 1) where functionno=@FUNID
		SELECT @intErrorCode = @@ERROR
		IF (@intErrorCode <> 0) GOTO PROBLEM
		
    COMMIT TRANSACTION
    
    PROBLEM:
	IF (@intErrorCode <> 0) 
		BEGIN
			ROLLBACK TRAN
			SET @TID=0
		END

Open in new window


I want to know the right approach as i need to use these for chat applications in msgs tables also where lots of records are inserted at a time by many users.
Please advise
Thanks in advance.
calorisplanitiaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
BurniePConnect With a Mentor Commented:
It depends.

Using Identity with auto increment you are 100% positive that every inserts will have a unique ID.  If you build it yourself, you need to be very cetain that if 2 inserts happen at the same time, you will not get the same generated ID which will cause trouble.  And by looking at the way you are doing it, you seem to be vulnerable to it.

I am personally using Identity with auto-increment in every table where it is required.  I never had any problems with lock issues, and never heard of any.
0
 
Ephraim WangoyaConnect With a Mentor Commented:

You are better ff using IDENTITY column
There is no need for adding overhead just to get a unique ID when SQL server is already optimized to do the function for you.

An identity column does not adversely affect your performance in any way
0
 
calorisplanitiaAuthor Commented:
What if there is a case like chat application where hundreds of msgs are inserted at a time?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
calorisplanitiaAuthor Commented:
will the table be available for view when the records are inserted, while using identity?
0
 
Ephraim WangoyaCommented:

Even in such a case, an Identity column will still perform better than generating your own unique id
0
 
BurniePCommented:
It is even more better to use identity if you have hundreds of message inserted at a time.  The table will be available for view since inserting is not locking existing rows.
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hi,

You have to use the identity column and also use this column with the concatenation with the userid + identity column and store it into any varchar type datafield and also define as unique value.
0
 
calorisplanitiaAuthor Commented:
Apologies,  i forgot to mark this question as completed.
I tested myself also and found that identity coulmns are much better.
Thanx to you all for helping me out.!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.