Home > database >  A custom function
A custom function

Time:09-27

Want to use MYSQL to do a custom function, but is always an error, please have a look at, be obliged!
 CREATE function GetCharIndexNum (` @ findstring ` varchar (255), ` @ string ` varchar (255)) 
Returns the int

The BEGIN
Declare ` @ location ` int.
Declare ` @ num ` int.

The set @ num=0;
The set @ location=charindex (@ findstring, @ string);
While @ the location & gt; 0 do
The begin
The set @ num=@ num + 1;
Set @ string=the substring (@ string, @ location + 1, len (@ string));
The set @ location=charindex (@ findstring, @ string);

End
Return the @ num

END

CodePudding user response:

Declare ` @ location ` int.
Variables don't add @

And should stick to the content of the error come out,

CodePudding user response:

The building Lord what database, what is error?

CodePudding user response:

reference 1st floor kxjrzyk response:
declare ` @ location ` int.
Variables don't add @

And should stick to the content of the error come out,
[SQL] the CREATE function GetCharIndexNum (` @ findstring ` varchar (255), ` @ string ` varchar (255))
Returns the int

The BEGIN
Declare ` @ location ` int.
Declare ` @ num ` int.

The set @ num=0;
The set @ location=charindex (@ findstring, @ string);
While @ the location & gt; 0 do
The begin
The set @ num=@ num + 1;
Set @ string=the substring (@ string, @ location + 1, len (@ string));
The set @ location=charindex (@ findstring, @ string);

End
Return the @ 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

At the line END '17

CodePudding user response:

Retrun @ num. Try with a semicolon

CodePudding user response:

Has given you change good, compile, you a lot of language use is wrong,
 
The CREATE FUNCTION ` GetCharIndexNum ` (` findstring ` VARCHAR (255),
` string ` VARCHAR (255))
RETURNS the INT
NO SQL
DETERMINISTIC
The BEGIN
DECLARE ` location ` INT.

DECLARE ` num ` INT.

The SET of num=0;

SET the location=charindex (findstring, string);

WHILE the location & gt; 0 DO
The SET num=num + 1;
SET the string=the Substring (string, the location + 1, len (string));
SET the location=Charindex (findstring, string);
END WHILE;

RETURN num.
END;

  • Related