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!
I think your problem is the overloading of the term teamName. Since teamName is a parameter to your procedure, the following query:
SELECT teamName FROM team
Is equivalent to:
SELECT 'foo' FROM team
Which will always return the string 'foo' once for each record in team (and therefore the IF 'foo' IN ... condition is always true). You would be well-advised to rename your parameter so that it doesn't collide with a column name.
Also, you need not perform the IF in the manner you are. Instead, why not:
IF (SELECT COUNT(*) FROM team WHERE teamName = new_parameter_name) THEN ...
As an aside, I prefer to raise a SQL error (e.g. by calling a non-existent procedure) rather than return a message string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With