简体   繁体   English

在MySQL中使用group_concat存储功能

[英]Stored Function with group_concat in MySQL

I want create a Stored Function in MySQL. 我想在MySQL中创建一个存储函数。 This Function have to include a group_concat function. 该函数必须包含一个group_concat函数。

Here is my current SQL Query: 这是我当前的SQL查询:

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; #1064-您的SQL语法有误; 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 检查与您的MariaDB服务器版本相对应的手册,以在第6行的'CONCAT('SELECT GROUP_CONCAT(DISTINCT',@ tableName.Sprache,'ORDER BY',@ table')附近使用正确的语法

Anybody have an idea? 有人有主意吗?

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,该动态SQL在MySQL中是通过准备好的语句来完成的,在PREPARE之后是EXECUTE ...。但是,在MySQL中,这些仅在存储过程中受支持,而在存储函数中不支持。

SQL syntax for prepared statements can be used within stored procedures, but not in stored functions or triggers. 准备语句的SQL语法可以在存储过程中使用,但不能在存储函数或触发器中使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM