循环方式一:
DELIMITER :; drop function if exists test.fn_test:; create function test.fn_test() returns varchar(32) begin declare temp varchar(36) default 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; declare str varchar(32) default ''; declare i int default 0; declare len int; while i < 32 do set i = i + 1; set len = CEIL(RAND() * 36); set str = CONCAT(str, SUBSTRING(temp, len, 1)); end while; return str; end:; DELIMITER; select fn_test();
循环方式二:
DELIMITER :; drop function if exists test.fn_test:; create function test.fn_test() returns varchar(32) begin declare temp varchar(36) default 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; declare str varchar(32) default ''; declare i int default 0; declare len int; loop_lable:LOOP if(i >= 32) then leave loop_lable; end if; set i = i + 1; set len = CEIL(RAND() * 36); set str = CONCAT(str, SUBSTRING(temp, len, 1)); end LOOP; return str; end:; DELIMITER; select fn_test();
循环方式三:
DELIMITER :; drop function if exists test.fn_test:; create function test.fn_test() returns varchar(32) begin declare temp varchar(36) default 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; declare str varchar(32) default ''; declare i int default 0; declare len int; REPEAT set i = i + 1; set len = CEIL(RAND() * 36); set str = CONCAT(str, SUBSTRING(temp, len, 1)); until i >= 32 end REPEAT; return str; end:; DELIMITER; select fn_test();