Answer the question
In order to leave comments, you need to log in
Transaction in MS SQL - how to check?
There are 2 tables. There is something like a purchase:
users
id_user, cash
items
id, id_user, item
At the beginning, the price is checked, the balance is checked, if it is possible to buy, then the following code is executed.
At the end we have a query like this:
BEGIN TRAN BUYITEM;
UPDATE users SET cash = [email protected] WHERE id = @id;
INSERT INTO items VALUES (@id, @item);
COMMIT TRAN BUYITEM;
BEGIN TRAN BUYITEM;
IF (SELECT cash FROM users where [email protected])<@cash RETURN;
UPDATE users SET cash = [email protected] WHERE id = @id;
INSERT INTO items VALUES (@id, @item);
COMMIT TRAN BUYITEM;
Answer the question
In order to leave comments, you need to log in
It is necessary to use a locking mechanism in addition to transactions.
For example, for MSSQL www.sql.ru/articles/mssql/2004/04110303advancedloc...
In your case it will be something like this:
BEGIN TRAN BUYITEM;
Select cash from users (UPDLOCK) WHERE id = @id;
UPDATE users SET cash = cash-@cash WHERE id = @id;
INSERT INTO items VALUES (@id, @item);
COMMIT TRAN BUYITEM;
Something I did not understand your tables.
I take them like:
users(id_user, name, cash)
items(id_item, item, price)
user_item(id_user,id_item)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question