Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need some help from u people.My senario like this

I am inserting multiple records at the same time users are hitting the same time. But here i found Running number from front end(Get the max number from database and adding 1 to that number) and adding this number in each and every row and forming insert query.


So before executing insert query if others are also getting the same number so how to handle this one.

Thanks & Regards,
Posted

Just add an integer ID field to the table that auto-increments.
 
Share this answer
 
v2
Yes, as John says let you database generate this number, if you need to use the number in the front end then just have your procedure return it after the insert has completed.

Something like...

SQL
CREATE PROCEDURE  example_procedure

	(
		@SomeVariable	INT,
		@AnoherVariable	INT,
		@UniqueFieldID	INT OUTPUT

	)

AS

SET NOCOUNT ON

INSERT INTO
	TheTable (Field1, Field2)
VALUES
	(@SomeVariable, @AnoherVariable)

--Return the new identity
SET @UniqueFieldID  =  SCOPE_IDENTITY()

-- Return any error codes \ Reset the NOCOUNT property.
RETURN @@ERROR
SET NOCOUNT OFF
GO
 
Share this answer
 
You can either turn the auto number on or you can also generate RANDOM NUMBER everytime as a primary key which will be diffrent for all the users.

No need to fetch the last record and add 1 in its id, instead just insert random number for all records which will be diffrent for all the users even if it wil be having entry in DB at same time.

(Random depends on the algorithm that you write)

Ashutosh Jain.
 
Share this answer
 
Comments
AspDotNetDev 14-Jul-10 14:39pm    
I recommend the IDENTITY field technique, but if somebody were taking the random number route, using a GUID would probably be the easiest way.

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