Advertisement
simonradev

Massive Shopping IDIOT Solution

Sep 16th, 2017
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.57 KB | None | 0 0
  1. DECLARE @username varchar(10) = 'Stamat';
  2. DECLARE @gameName varchar(10) = 'Safflower';
  3. DECLARE @gameId int = (SELECT Id
  4.                         FROM Games
  5.                         WHERE Name = @gameName);
  6. DECLARE @usersGameId int = (SELECT ug.Id
  7.                              FROM UsersGames AS ug
  8.                              INNER JOIN Games AS g
  9.                              ON ug.GameId = @gameId AND
  10.                                 ug.GameId = g.Id
  11.                              INNER JOIN Users AS u
  12.                              ON u.Id = ug.UserId AND
  13.                                 u.Username = @username);
  14.  
  15. -------------------------------------------------------------------------------- FIRST
  16.     DECLARE item_Cursor CURSOR FOR
  17.     SELECT i.Id, i.Price
  18.      FROM Items AS i
  19.      WHERE i.MinLevel BETWEEN 11 AND 12
  20.  
  21.     OPEN item_Cursor
  22.  
  23.     DECLARE @currentItemId int;
  24.     DECLARE @currentItemPrice money;
  25.  
  26.     FETCH NEXT FROM item_Cursor
  27.     INTO @currentItemId, @currentItemPrice
  28.  
  29.     DECLARE @currentCashOfUser money = (SELECT Cash
  30.                                          FROM UsersGames
  31.                                          WHERE Id = @usersGameId);
  32.     DECLARE @usersGameCashAfterBuys money = 0;
  33.     DECLARE @purchaseIsSuccessfull bit = 1;
  34.  
  35.     DECLARE @tableToInsertIntoUserGameItems TABLE (ItemId int, UserGameId int);
  36.  
  37.     WHILE @@FETCH_STATUS = 0
  38.     BEGIN
  39.         --INSERT INTO UserGameItems (ItemId, UserGameId)
  40.         --VALUES (@currentItemId, @usersGameId)
  41.         INSERT INTO @tableToInsertIntoUserGameItems
  42.         VALUES (@currentItemId, @usersGameId)
  43.        
  44.         SET @usersGameCashAfterBuys += @currentItemPrice;
  45.  
  46.         IF(@usersGameCashAfterBuys > @currentCashOfUser)
  47.         BEGIN
  48.          SET @purchaseIsSuccessfull = 0;
  49.          BREAK;
  50.         END
  51.        
  52.         FETCH NEXT FROM item_Cursor
  53.         INTO @currentItemId, @currentItemPrice
  54.     END
  55.  
  56.     CLOSE item_Cursor
  57.     DEALLOCATE item_Cursor
  58.  
  59. IF(@purchaseIsSuccessfull = 1)
  60. BEGIN
  61.     INSERT INTO UserGameItems
  62.      SELECT t.ItemId, t.UserGameId FROM @tableToInsertIntoUserGameItems AS t
  63.    
  64.     UPDATE UsersGames
  65.      SET Cash -= @usersGameCashAfterBuys
  66.      WHERE Id = @usersGameId
  67. END
  68. -------------------------------------------------------------------------------- FIRST
  69.  
  70. -------------------------------------------------------------------------------- SECOND
  71.     DECLARE item_Cursor CURSOR FOR
  72.     SELECT i.Id, i.Price
  73.      FROM Items AS i
  74.      WHERE i.MinLevel BETWEEN 19 AND 21
  75.  
  76.     OPEN item_Cursor
  77.  
  78.     SET @currentItemId = 0;
  79.     SET @currentItemPrice = 0;
  80.  
  81.     FETCH NEXT FROM item_Cursor
  82.     INTO @currentItemId, @currentItemPrice
  83.  
  84.     SET @currentCashOfUser = (SELECT Cash
  85.                                 FROM UsersGames
  86.                                 WHERE Id = @usersGameId);
  87.     SET @usersGameCashAfterBuys = 0;
  88.     SET @purchaseIsSuccessfull = 1;
  89.  
  90.     DECLARE @secondTableToInsertIntoUserGameItems TABLE (ItemId int, UserGameId int);
  91.  
  92.     WHILE @@FETCH_STATUS = 0
  93.     BEGIN
  94.         --INSERT INTO UserGameItems (ItemId, UserGameId)
  95.         --VALUES (@currentItemId, @usersGameId)
  96.         INSERT INTO @secondTableToInsertIntoUserGameItems
  97.         VALUES (@currentItemId, @usersGameId)
  98.        
  99.         SET @usersGameCashAfterBuys += @currentItemPrice;
  100.  
  101.         IF(@usersGameCashAfterBuys > @currentCashOfUser)
  102.         BEGIN
  103.          SET @purchaseIsSuccessfull = 0;
  104.          BREAK;
  105.         END
  106.        
  107.         FETCH NEXT FROM item_Cursor
  108.         INTO @currentItemId, @currentItemPrice
  109.     END
  110.  
  111.     CLOSE item_Cursor
  112.     DEALLOCATE item_Cursor
  113.  
  114. IF(@purchaseIsSuccessfull = 1)
  115. BEGIN
  116.     INSERT INTO UserGameItems
  117.      SELECT t.ItemId, t.UserGameId FROM @secondTableToInsertIntoUserGameItems AS t
  118.    
  119.     UPDATE UsersGames
  120.      SET Cash -= @usersGameCashAfterBuys
  121.      WHERE Id = @usersGameId
  122. END
  123. -------------------------------------------------------------------------------- SECOND
  124.  
  125. SELECT i.Name
  126.  FROM Games AS g
  127.  INNER JOIN UsersGames AS ug
  128.  ON g.Id = ug.GameId AND
  129.     g.Id = 87
  130.  INNER JOIN UserGameItems AS ugi
  131.  ON ug.Id = ugi.UserGameId
  132.  INNER JOIN Items AS i
  133.  ON ugi.ItemId = i.Id
  134.  ORDER BY i.Name ASC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement