I am trying to build a dynamic variable name in a user-defined function, but it seems that it does not work. Is there a way to do this or using an array variable?
I have a 7 character string that represents the days of the week (1234567 or 1_3_5_7, etc.). I would like to evaluate how often a day is selected during the week (from 0 to 7). I thought it would be easier to use a loop to go through all 7 positions, but I get an error
[Err] 1193 - Unknown system variable 'CONCAT'
Any hints on how I can achieve this? This is my code:
DELIMITER $$
DROP FUNCTION IF EXISTS fn_freq$$
CREATE FUNCTION fn_freq(days INT) RETURNS INT
BEGIN
DECLARE D1 VARCHAR(1);
DECLARE D2 VARCHAR(1);
DECLARE D3 VARCHAR(1);
DECLARE D4 VARCHAR(1);
DECLARE D5 VARCHAR(1);
DECLARE D6 VARCHAR(1);
DECLARE D7 VARCHAR(1);
DECLARE x INT;
DECLARE fn_freq INT;
SET x =1;
SET fn_freq = 0;
WHILE x < 8 DO
SET CONCAT('D',x) = MID(days, x, 1);
IF CONCAT('D',x) = '_' THEN
ELSE
SET fn_freq = fn_freq + 1;
SET x = x + 1;
END IF;
SET x = x + 1;
END WHILE;
RETURN fn_freq;
END$$
DELIMITER ;
source
share