Click here to Skip to main content
16,011,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to update a Records in Sql Server at same time on the same records by two user ?
Posted

The best practice is Transaction.

So use transaction.

Check this out:

CREATE PROCEDURE InsertRecords
(
@parameter1 varchar (50),
@parameter2 varchar (50),
.
.

@parametern varchar(50)
)
AS
BEGIN

BEGIN TRANSACTION
BEGIN TRY
INSERT INTO [tablename]
(Field1,Field2,......Fieldn)
VALUES
(@parameter1,@parameter2,......@parametern)

COMMIT TRANSACTION
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_MESSAGE() AS ErrorMessage
END CATCH
END


If any of the two users request fails, it will roll back and ask the user to resubmit his/her request.


Hope this helps.
 
Share this answer
 
v2
Short answer is you can't.

If two users/processes try to update the same data at the same time a deadlock will occur, when this happens one of the processes will be chosen as the victim and will fail. What you need to do is catch this error (error code 1205) and retry the command again.
 
Share this answer
 
you can use multiple update queries..



like



first update query

seperated by ;(semicolon)


second update query
 
Share this answer
 
v3

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