Stored Function with group_concat in MySQL

590 Views Asked by At

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?

1

There are 1 best solutions below

0
On

You are trying to use dynamic SQL, which in MySQL is done using prepared statements, with PREPARE followed by EXECUTE.... however, in MySQL, these are only supported in stored procedures, not stored functions.

SQL syntax for prepared statements can be used within stored procedures, but not in stored functions or triggers.

http://dev.mysql.com/doc/refman/5.6/en/sql-syntax-prepared-statements.html