Drop function if exists rand_string;
Delimiter //
Create Function rand_string (l_num tinyint unsigned, l_type tinyint unsigned)
Returns varchar (127)
Begin
-- Function: rand_string
-- Author: dbachina # dbachina.com
-- Date: 2010/5/30
-- L_num: the length of random string
-- L_type: the string type
-- 1.0-9
-- 2. A-z
-- 3. A-Z
-- 4. A-Za-z
-- 5.0-9a-za-z
-- <For example>:
-- Mysql> select rand_string (12, 5) random_string;
-- + --------------- +
-- | Random_string |
-- + --------------- +
-- | 3 kzgjcujuplw |
-- + --------------- +
-- 1 row in SET (0.00 Sec)
Declare I int unsigned default 0;
Declare v_chars varchar (64) default '20140901 ';
Declare result varchar (255) default '';
If l_type = 1 then
Set v_chars = '20140901 ';
Elseif l_type = 2 then
Set v_chars = 'abcdefghijklmnopqrstuvwxy ';
Elseif l_type = 3 then
Set v_chars = 'abcdefghijklmnopqrstuvwxy ';
Elseif l_type = 4 then
Set v_chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
Elseif l_type = 5 then
Set v_chars = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
Else
Set v_chars = '20140901 ';
End if;
While I <l_num do
Set result = Concat (result, substr (v_chars, Ceil (RAND () * (length (v_chars)-1), 1 ));
Set I = I + 1;
End while;
Return result;
End;
//
Delimiter;