开发者

MySQL Return GROUP_CONCAT in FUNCTION

开发者 https://www.devze.com 2023-01-27 05:20 出处:网络
I\'m using MySQL Server 7.0 on Windows Server 2008 and am trying to return the result of a GROUP_CONCAT in a function . General SQL is as follows:

I'm using MySQL Server 7.0 on Windows Server 2008 and am trying to return the result of a GROUP_CONCAT in a function . General SQL is as follows:

DELIMITER 开发者_运维技巧;

DROP FUNCTION IF EXISTS MyFunction;
DELIMITER $$

CREATE FUNCTION MyFunction(MyVar INT)
    RETURNS VARCHAR(255)
BEGIN
    SELECT @MyRetVar = GROUP_CONCAT(MyColumn)
    FROM   MyTable
 WHERE  MyID = MyVar;
    RETURN @MyRetVar;
END$$

DELIMITER ;

This yields the following result:

ERROR 1415 (0A000): Not allowed to return a result set from a function

I checked the manual (http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html) and read

The result type is TEXT or BLOB unless group_concat_max_len is less than or equal to 512, in which case the result type is VARCHAR or VARBINARY.

I changed the value of group_concat_max_len from its default value to 512 and also 256 in My.ini (and restarted the MySQL service). I've verified the change using

mysql> show variables like "%concat%";

Any help is appreciated!


Try

SELECT GROUP_CONCAT(MyColumn)
FROM   MyTable
WHERE  MyID = MyVar INTO @MyRetVar;
RETURN @MyRetVar;
0

精彩评论

暂无评论...
验证码 换一张
取 消