Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.71 KB | None | 0 0
  1. CREATE TRIGGER tr_ItemsPurchaseRestrictions ON UserGameItems FOR INSERT
  2. AS
  3. BEGIN
  4.  
  5.   DECLARE @userGameLevel int = (
  6.  
  7.     SELECT ug.Level
  8.  
  9.     FROM inserted AS ugi -- UserGameItems
  10.  
  11.     JOIN UsersGames AS ug ON ugi.UserGameId = ug.Id
  12.  
  13.   );
  14.  
  15.   DECLARE @itemMinLevel int = (
  16.  
  17.     SELECT i.MinLevel
  18.  
  19.     FROM inserted AS ugi -- UserGameItems
  20.  
  21.     JOIN Items AS i on i.Id = ugi.ItemId
  22.  
  23.   );
  24.  
  25.   IF(@itemMinLevel > @userGameLevel)
  26.  
  27.     BEGIN
  28.       ROLLBACK;
  29.  
  30.       RAISERROR('Higher user game level required for item purchase', 16, 1);
  31.  
  32.       RETURN;
  33.  
  34.     END
  35. END
  36. --testing
  37. INSERT INTO UserGameItems (ItemId, UserGameId) VALUES (3, 2);
  38. INSERT INTO UserGameItems (ItemId, UserGameId) VALUES (3, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement