I want create a Stored Function in MySQL. This Function have to include a group_concat function.
Here is my current SQL Query:
DELIMITER $$
CREATE FUNCTION SprachenListe(
paramTable VARCHAR( 50 )
) RETURNS TEXT DETERMINISTIC BEGIN
DECLARE Ausgabe TEXT;
SET @tableName = paramTable;
EXECUTE IMMEDIATE CONCAT('SELECT GROUP_CONCAT( DISTINCT ', @tableName.Sprache, ' ORDER
BY ',@tableName.Sprache,' SEPARATOR ','/',' ) INTO ',Ausgabe,' FROM ',@tableName);
RETURN(Ausgabe);
END ;
$$
DELIMITER ;
Here is Error Message:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CONCAT('SELECT GROUP_CONCAT( DISTINCT ', @tableName.Sprache, ' ORDER BY ',@table' at line 6
Anybody have an idea?
You are trying to use dynamic SQL, which in MySQL is done using prepared statements, with
PREPARE
followed byEXECUTE
.... however, in MySQL, these are only supported in stored procedures, not stored functions.