I've created a stored procedure in MySQL (server version: 5.0.83-community) as shown below. When I try to call it I get a really strange output that differs a whole lot from when I try to call the query outside the procedure. To show what I mean, I've unioned the two different calls so you can see the very different outputs.
I know that a CONCAT will return a NULL variable if any argument is NULL as explained in CONCAT in stored procedure returns null, but I really don't understand why it returns NULL in one scenario as below while not in the other. In fact, both callings should return the exact same result. Any ideas on why this happens? (The delimiter // is used by phpMyAdmin - hence the weird lack of declaration).
NOTE: I've also looked at Stored Procedure - OUT param returns null when selected and Mysql - Stored procedure OUT variable return null but none of them lead me into any sightful conclusions. Nor does this article help me Why does this query return different results when in a stored procedure? since MySQL throws this error when I try to invoke SET ANSI_NULLS ON; #1193 - Unknown system variable 'ANSI_NULLS'.
DROP PROCEDURE `GetVoteID`//
CREATE DEFINER=`u1037413_manager`@`%` PROCEDURE `GetVoteID`(IN minimum_votes INT(11), IN current_round INT(11), INOUT vote_id CHAR(11))
BEGIN
SET @eliminated_candidates := (
SELECT GROUP_CONCAT(CONVERT(vote_candidate, CHAR(11)))
FROM vote_final
WHERE votes = minimum_votes
AND round_id = current_round
);
SELECT GROUP_CONCAT(CONVERT(vote_id, CHAR(11))) INTO vote_id
FROM votes_copy
WHERE FIND_IN_SET(@eliminated_candidates, vote_candidate_1) != 0;
END
CALL GetVoteID('5','1',@vote_id);
SELECT *
FROM (
(
SELECT @vote_id vote_id
) UNION (
SELECT GROUP_CONCAT(CONVERT(vote_id, CHAR(11)))
FROM votes_copy
WHERE FIND_IN_SET((
SELECT GROUP_CONCAT(CONVERT(vote_candidate, CHAR(11)))
FROM vote_final
WHERE votes = '5'
AND round_id = '1'
), vote_candidate_1) != 0
)
) t;
Outputs:
+--------------+
| vote_id |
+--------------+
| NULL | <- incorrect result
+--------------+
| 2,8,33,44,48 | <- correct result
+--------------+