morgan73

Buy Items for User in Game

Oct 21st, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.17 KB | None | 0 0
  1. DECLARE @userId INT = (SELECT Id FROM Users
  2.                         WHERE Username = 'Alex')
  3. DECLARE @gameId INT = (SELECT Id FROM Games
  4.                         WHERE Name = 'Edinburgh')
  5. DECLARE @userGameId INT = (SELECT Id FROM UsersGames
  6.                         WHERE UserId = @userId AND GameId = @gameId)
  7. DECLARE @itemsSum DECIMAL(15, 2) = (
  8.     select SUM(Price) from Items
  9. where Name in ('Blackguard', 'Bottomless Potion of Amplification',
  10.                 'Eye of Etlich (Diablo III)', 'Gem of Efficacious Toxin',
  11.                 'Golden Gorget of Leoric', 'Hellfire Amulet'))
  12.  
  13. BEGIN TRY
  14. BEGIN TRANSACTION
  15.     UPDATE UsersGames
  16.     SET Cash -= @itemsSum
  17.     WHERE Id = @userGameId
  18.  
  19.     INSERT INTO UserGameItems
  20.     SELECT Id, @userGameId FROM Items
  21.     WHERE Name in ('Blackguard', 'Bottomless Potion of Amplification',
  22.                 'Eye of Etlich (Diablo III)', 'Gem of Efficacious Toxin',
  23.                 'Golden Gorget of Leoric', 'Hellfire Amulet')
  24. COMMIT
  25. END TRY
  26. BEGIN CATCH
  27.     ROLLBACK
  28. END CATCH
  29.  
  30. SELECT u.Username, g.Name, ug.Cash, i.Name AS [Item Name]
  31. FROM UsersGames AS ug
  32. JOIN Games AS g ON ug.GameId = g.Id
  33. JOIN Users AS u ON ug.UserId = u.Id
  34. JOIN UserGameItems AS ugi ON ug.Id = ugi.UserGameId
  35. JOIN Items AS i ON i.Id = ugi.ItemId
  36. WHERE g.Name = 'Edinburgh'
Add Comment
Please, Sign In to add comment