想用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
uj5u.com熱心網友回復:
declare `@location` int ;變數不要加@
還有應該把報錯的內容也貼出來吧。
uj5u.com熱心網友回復:
樓主用的什么資料庫,錯誤提示是什么?uj5u.com熱心網友回復:
[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
uj5u.com熱心網友回復:
retrun @num;加分號試一下uj5u.com熱心網友回復:
已給你改好了,編譯通過,你的不少語言用錯了。
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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/85501.html
標籤:MySQL
下一篇:MYSQL自定義函式報1064錯
