text: USE [Monopoly]
  GO
  
  /****** Object:  StoredProcedure [Player].[uspReceiveAmount]    Script Date: 2/19/2022 10:40:00 AM ******/
  SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER ON
  GO
  
  CREATE PROC [Player].[uspReceiveAmount]
  	@player int,
  	@amount int
  AS
  BEGIN
  	SET NOCOUNT ON;
  
  	-- Get current amount for each unique bill
  	DECLARE @cash_500 SMALLINT,
  		@cash_100 SMALLINT,
  		@cash_50 SMALLINT,
  		@cash_20 SMALLINT,
  		@cash_10 SMALLINT,
  		@cash_5 SMALLINT,
  		@cash_1 SMALLINT;
  	SELECT @cash_500 = [cash_500],
  		@cash_100 = [cash_100],
  		@cash_50 = [cash_50],
  		@cash_20 = [cash_20],
  		@cash_10 = [cash_10],
  		@cash_5 = [cash_5],
  		@cash_1 = [cash_1]
  	FROM [Player].[Players]
  	WHERE [rowid] = @player;
  
  	-- Break amount into unique bills
  	DECLARE @temp_amount INT = @amount;
  	WHILE @temp_amount > 0
  	BEGIN
  		IF @temp_amount >= 500
  			BEGIN
  				SET @cash_500 += 1;
  				SET @temp_amount -= 500;
  			END
  		ELSE IF @temp_amount >= 100
  			BEGIN
  				SET @cash_100 += 1;
  				SET @temp_amount -= 100;
  			END
  		ELSE IF @temp_amount >= 50
  			BEGIN
  				SET @cash_50 += 1;
  				SET @temp_amount -= 50;
  			END
  		ELSE IF @temp_amount >= 20
  			BEGIN
  				SET @cash_20 += 1;
  				SET @temp_amount -= 20;
  			END
  		ELSE IF @temp_amount >= 10
  			BEGIN
  				SET @cash_10 += 1;
  				SET @temp_amount -= 10;
  			END
  		ELSE IF @temp_amount >= 5
  			BEGIN
  				SET @cash_5 += 1;
  				SET @temp_amount -= 5;
  			END
  		ELSE
  			BEGIN
  				SET @cash_1 += 1;
  				SET @temp_amount -= 1;
  			END
  	END;
  
  	-- Set amount of each unique bill to new value
  	UPDATE [Player].[Players]
  	SET [cash_500] = @cash_500,
  		[cash_100] = @cash_100,
  		[cash_50] = @cash_50,
  		[cash_20] = @cash_20,
  		[cash_10] = @cash_10,
  		[cash_5] = @cash_5,
  		[cash_1] = @cash_1
  	WHERE [rowid] = @player;
  END
  GO
  
  
  