DELIMITER $$ USE `t_girl`$$ DROP FUNCTION IF EXISTS `func_range_string_mod`$$ CREATE DEFINER=`root`@`localhost` FUNCTION `func_range_string_mod`( f_num INT UNSIGNED -- Total strings. ) RETURNS VARCHAR(200) CHARSET latin1 BEGIN DECLARE i INT UNSIGNED DEFAULT 0; DECLARE v_result VARCHAR(200) DEFAULT ''; DECLARE v_dict VARCHAR(200) DEFAULT ''; SET v_dict = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; SET v_dict = LPAD(v_dict,200,v_dict); WHILE i < f_num DO SET v_result = CONCAT(v_result,SUBSTR(v_dict,CEIL(RAND()*200),1)); SET i = i + 1; END WHILE; RETURN v_result; END$$ DELIMITER ;
調用方法:
select func_range_string_mod(20); 代表需要生成20個字符長度的密碼。