text: USE [Monopoly]
  GO
  
  /****** Object:  StoredProcedure [Player].[uspAddPlayer]    Script Date: 2/19/2022 10:34:51 AM ******/
  SET ANSI_NULLS ON
  GO
  
  SET QUOTED_IDENTIFIER ON
  GO
  
  
  
  CREATE PROC [Player].[uspAddPlayer]
  	@player_name varchar(20),
  	@token int
  AS
  BEGIN
  	SET NOCOUNT ON;
  
  	-- Get current state of player list
  	DECLARE @num_tokens SMALLINT, @num_players SMALLINT;
  	SELECT @num_tokens = COUNT([rowid]) FROM [Game].[Tokens];
  	SELECT @num_players = COUNT([rowid]) FROM [Player].[Players];
  
  	-- Check if all tokens have already been selected
  	IF @num_players = @num_tokens
  		THROW 50001, 'The maximum number of players has already been reached.', 1;
  
  	-- Check that no other player has the same token
  	IF @token IN (SELECT [token] FROM [Player].[Players])
  		THROW 50001, 'Token has already been chosen by another player.', 1;
  
  	-- Add player to the game
  	INSERT INTO [Player].[Players] ([player_name], [token])
  	VALUES (@player_name, @token);
  END
  GO
  
  
  