K tomu nepotřebujete funkci, potřebujete uloženou proceduru.
DELIMITER $$
CREATE PROCEDURE proc_name(IN col_number1 INT, IN col_number2 INT)
BEGIN
SET @col1 := CONCAT('CHR_MORNING', col_number1);
SET @col2 := CONCAT('CHR_EVENING', col_number2);
SET @sql := CONCAT('
SELECT
CASE WHEN ', @col1, '="SUN" && ', @col2, '="SUN" THEN "S"
WHEN (', @col1, '="HOL" && ', @col2, '="HOL") THEN "H"
WHEN (', @col1, '="LEA" && ', @col2, '="LEA") THEN "L"
WHEN (', @col1, '="ABS" && ', @col2, '="ABS") THEN "-"
WHEN (', @col1, '="NA" && ', @col2, '="NA") THEN "-"
ELSE "8"
END AS whatever_you_want_to_name_your_column
FROM att_t_register WHERE CHR_MONTH = "jan" AND INT_YEAR = 2014;
');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER ;
Pak byste to provedli pomocí
CALL proc_name(1, 1);