FUNCTION `test`.`fn_find` (
find VARCHAR (8000),
str VARCHAR (8000),
n SMALLINT
) RETURNS INT
/*LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT “string”*/
BEGIN
DECLARE dstart INT DEFAULT 0 ;
DECLARE dcount INT DEFAULT 0 ;
DECLARE dindex INT DEFAULT 0 ;
DECLARE dlen INT DEFAULT 0 ;
find VARCHAR (8000),
str VARCHAR (8000),
n SMALLINT
) RETURNS INT
/*LANGUAGE SQL
| [NOT] DETERMINISTIC
| { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
| SQL SECURITY { DEFINER | INVOKER }
| COMMENT “string”*/
BEGIN
DECLARE dstart INT DEFAULT 0 ;
DECLARE dcount INT DEFAULT 0 ;
DECLARE dindex INT DEFAULT 0 ;
DECLARE dlen INT DEFAULT 0 ;
IF n < 1 THEN RETURN (0) ;
ELSE
SET dindex = LOCATE(find, str) ;
IF dindex = 0 THEN RETURN (0) ;
ELSE
SELECT
dcount = 1,
dlen = LENGTH(find) ;
END IF ;
WHILE (dindex > 0 AND dcount < n)
DO
SET dstart = dindex + dlen ;
SELECT dindex = LOCATE(find, str, dstart),
dcount = dcount + 1 ;
END WHILE;
IF dcount < n
THEN SET dindex = 0;
END IF ;
END IF ;
RETURN (dindex);
END $$
DELIMITER ;
这个函数执行报错:错误代码: 1415 Not allowed to return a result set from a function
找了好久不知道哪里出错了,求高手帮看下,谢谢!
解决方案
20
一眼看不出什么问题。 先把 RETURN (dindex); 上的括号去掉再试试。
10
那应该怎么赋值?SET dindex := LOCATE(find, str) ;
10
多谢LZ分享,受益匪浅