This is probably a trivial problem for most of you... Below is a stored function I have created in MySQL:
DROP PROCEDURE IF EXISTS teamSize;
DELIMITER //
CREATE PROCEDURE teamSize(IN teamName VARCHAR(50), IN teamYear INT(4), OUT noOfPlayers INT)
BEGIN
IF teamName IN (SELECT teamName FROM team) THEN
SELECT COUNT(playerID) INTO noOfPlayers
FROM team
JOIN teamAllocation
ON teamAllocation.teamID = team.teamID
WHERE team.teamName = teamName
AND team.teamYear = teamYear;
ELSE
SELECT "That team does not exist in the database" AS "Error Message";
END IF;
END //
DELIMITER ;
I made this function so I can calculate the size of a given team in a given year. The team name and the team year are parsed in as parameter values. For this function I want a user friendly error message to display if the team name the user typed in does not exist in the database.
For example: I type in a team that does exist I get the following results, as I expected:
mysql> CALL teamSize("U11 Orange", 2012, @noOfPlayers);
--------------
CALL teamSize("U11 Orange", 2012, @noOfPlayers)
--------------
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @noOfPlayers;
--------------
SELECT @noOfPlayers
--------------
+--------------+
| @noOfPlayers |
+--------------+
| 2 |
+--------------+
1 row in set (0.00 sec)
mysql>
...But if I type in a team that I know doesn't exist in the database I get the following:
mysql> CALL teamSize("jkgefkrkvbey", 2012, @teamSize);
--------------
CALL teamSize("jkgefkrkvbey", 2012, @teamSize)
--------------
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT @teamSize;
--------------
SELECT @teamSize
--------------
+-----------+
| @teamSize |
+-----------+
| 0 |
+-----------+
1 row in set (0.00 sec)
mysql>
What I am wanting for the case where a team name parsed into the function doesn't exist is something like the following:
+------------------------------------------+
| Error Message |
+------------------------------------------+
| That team does not exist in the database |
+------------------------------------------+
1 row in set (0.00 sec)
mysql>
If anyone has some feedback or advice on how I can achieve the above that would be very much appreciated.
Thanks in advance!