I am trying to create a stored procedure to obtain the median age within a table but am getting an undeclared variable error. My code is:
DELIMITER //
CREATE PROCEDURE MedianAge()
BEGIN
SET @row_count = (SELECT COUNT(*) FROM employee);
SET @median_index = (@row_count/2);
SELECT TIMESTAMPDIFF(YEAR, bdate, CURDATE()) AS age
FROM employee ORDER BY bdate DESC
LIMIT median_index, median_index;
END //
DELIMITER ;
I am receiving the error:
Error Code: 1327. Undeclared variable: median_index
As far as I am aware I have declared the variable correctly and am unsure why the SELECT statement does not work.