自定义函数问题

yuzhifu1 2018-07-04 07:12:56
想用MYSQL做一个自定义函数,但老是报错,请帮忙看一下,不胜感激!
CREATE  function GetCharIndexNum (`@findstring` varchar(255),`@string` varchar(255))
returns int

BEGIN
declare `@location` int ;
declare `@num` int ;

set @num =0;
set @location = charindex (@findstring,@string);
while @location >0 do
begin
set @num =@num +1;
set @string =substring(@string,@location+1,len(@string));
set @location = charindex (@findstring,@string);

end
return @num

END
...全文
244 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
生命沉思者 2018-07-05
  • 打赏
  • 举报
回复
已给你改好了,编译通过,你的不少语言用错了。

CREATE FUNCTION `GetCharIndexNum`(`findstring` VARCHAR(255),
`string` VARCHAR(255))
RETURNS INT
NO SQL
DETERMINISTIC
BEGIN
DECLARE `location` INT;

DECLARE `num` INT;

SET num =0;

SET location = charindex (findstring, string);

WHILE location > 0 DO
SET num = num +1;
SET string = Substring(string, location+1, len(string));
SET location = Charindex (findstring, string);
END WHILE;

RETURN num;
END;

Rotel-刘志东 2018-07-05
  • 打赏
  • 举报
回复
retrun @num;加分号试一下
yuzhifu1 2018-07-05
  • 打赏
  • 举报
回复
引用 1 楼 kxjrzyk 的回复:
declare `@location` int ;
变量不要加@

还有应该把报错的内容也贴出来吧。
[SQL] CREATE function GetCharIndexNum (`@findstring` varchar(255),`@string` varchar(255))
returns int

BEGIN
declare `@location` int ;
declare `@num` int ;

set @num =0;
set @location = charindex (@findstring,@string);
while @location >0 do
begin
set @num =@num +1;
set @string =substring(@string,@location+1,len(@string));
set @location = charindex (@findstring,@string);

end
return @num

END

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'return @num

END' at line 17
ACMAIN_CHM 2018-07-04
  • 打赏
  • 举报
回复
楼主用的什么数据库,错误提示是什么?
生命沉思者 2018-07-04
  • 打赏
  • 举报
回复
declare `@location` int ;
变量不要加@

还有应该把报错的内容也贴出来吧。

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧