I'm trying to get a random string in phpmyadmin using a function.
I have the following code:
CREATE FUNCTION randomPassword()
RETURNS varchar(128)
BEGIN
SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
SET @charLen = length(@chars);
SET @randomPassword = '';
WHILE length(@randomPassword) < 12
SET @randomPassword = concat(@randomPassword, substring(@chars,CEILING(RAND() * @charLen),1));
END WHILE;
RETURN @randomPassword ;
END;
Now I get the error:
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 '' at line 5
Does anyone know how I can fix this?
解决方案
It's solved by using the Delimiter, i don't know for sure how, but it works
Thanks
DELIMITER $$
CREATE FUNCTION randomPassword()
RETURNS varchar(128)
BEGIN
SET @chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
SET @charLen = length(@chars);
SET @randomPassword = '';
WHILE length(@randomPassword) < 12
DO
SET @randomPassword = concat(@randomPassword, substring(@chars,CEILING(RAND() * @charLen),1));
END WHILE;
RETURN @randomPassword ;
END$$
DELIMITER ;