Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DECLARE @username varchar(10) = 'Stamat';
- DECLARE @gameName varchar(10) = 'Safflower';
- DECLARE @gameId int = (SELECT Id
- FROM Games
- WHERE Name = @gameName);
- DECLARE @usersGameId int = (SELECT ug.Id
- FROM UsersGames AS ug
- INNER JOIN Games AS g
- ON ug.GameId = @gameId AND
- ug.GameId = g.Id
- INNER JOIN Users AS u
- ON u.Id = ug.UserId AND
- u.Username = @username);
- -------------------------------------------------------------------------------- FIRST
- DECLARE item_Cursor CURSOR FOR
- SELECT i.Id, i.Price
- FROM Items AS i
- WHERE i.MinLevel BETWEEN 11 AND 12
- OPEN item_Cursor
- DECLARE @currentItemId int;
- DECLARE @currentItemPrice money;
- FETCH NEXT FROM item_Cursor
- INTO @currentItemId, @currentItemPrice
- DECLARE @currentCashOfUser money = (SELECT Cash
- FROM UsersGames
- WHERE Id = @usersGameId);
- DECLARE @usersGameCashAfterBuys money = 0;
- DECLARE @purchaseIsSuccessfull bit = 1;
- DECLARE @tableToInsertIntoUserGameItems TABLE (ItemId int, UserGameId int);
- WHILE @@FETCH_STATUS = 0
- BEGIN
- --INSERT INTO UserGameItems (ItemId, UserGameId)
- --VALUES (@currentItemId, @usersGameId)
- INSERT INTO @tableToInsertIntoUserGameItems
- VALUES (@currentItemId, @usersGameId)
- SET @usersGameCashAfterBuys += @currentItemPrice;
- IF(@usersGameCashAfterBuys > @currentCashOfUser)
- BEGIN
- SET @purchaseIsSuccessfull = 0;
- BREAK;
- END
- FETCH NEXT FROM item_Cursor
- INTO @currentItemId, @currentItemPrice
- END
- CLOSE item_Cursor
- DEALLOCATE item_Cursor
- IF(@purchaseIsSuccessfull = 1)
- BEGIN
- INSERT INTO UserGameItems
- SELECT t.ItemId, t.UserGameId FROM @tableToInsertIntoUserGameItems AS t
- UPDATE UsersGames
- SET Cash -= @usersGameCashAfterBuys
- WHERE Id = @usersGameId
- END
- -------------------------------------------------------------------------------- FIRST
- -------------------------------------------------------------------------------- SECOND
- DECLARE item_Cursor CURSOR FOR
- SELECT i.Id, i.Price
- FROM Items AS i
- WHERE i.MinLevel BETWEEN 19 AND 21
- OPEN item_Cursor
- SET @currentItemId = 0;
- SET @currentItemPrice = 0;
- FETCH NEXT FROM item_Cursor
- INTO @currentItemId, @currentItemPrice
- SET @currentCashOfUser = (SELECT Cash
- FROM UsersGames
- WHERE Id = @usersGameId);
- SET @usersGameCashAfterBuys = 0;
- SET @purchaseIsSuccessfull = 1;
- DECLARE @secondTableToInsertIntoUserGameItems TABLE (ItemId int, UserGameId int);
- WHILE @@FETCH_STATUS = 0
- BEGIN
- --INSERT INTO UserGameItems (ItemId, UserGameId)
- --VALUES (@currentItemId, @usersGameId)
- INSERT INTO @secondTableToInsertIntoUserGameItems
- VALUES (@currentItemId, @usersGameId)
- SET @usersGameCashAfterBuys += @currentItemPrice;
- IF(@usersGameCashAfterBuys > @currentCashOfUser)
- BEGIN
- SET @purchaseIsSuccessfull = 0;
- BREAK;
- END
- FETCH NEXT FROM item_Cursor
- INTO @currentItemId, @currentItemPrice
- END
- CLOSE item_Cursor
- DEALLOCATE item_Cursor
- IF(@purchaseIsSuccessfull = 1)
- BEGIN
- INSERT INTO UserGameItems
- SELECT t.ItemId, t.UserGameId FROM @secondTableToInsertIntoUserGameItems AS t
- UPDATE UsersGames
- SET Cash -= @usersGameCashAfterBuys
- WHERE Id = @usersGameId
- END
- -------------------------------------------------------------------------------- SECOND
- SELECT i.Name
- FROM Games AS g
- INNER JOIN UsersGames AS ug
- ON g.Id = ug.GameId AND
- g.Id = 87
- INNER JOIN UserGameItems AS ugi
- ON ug.Id = ugi.UserGameId
- INNER JOIN Items AS i
- ON ugi.ItemId = i.Id
- ORDER BY i.Name ASC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement