前置知识
创建一个随机产生字符串的函数rand_string(int);
DELIMITER $$
CREATE FUNCTION rand_string(n INT) RETURNS VARCHAR(255)
BEGIN
DECLARE chars_str VARCHAR(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
DECLARE return_str VARCHAR(255) DEFAULT '';
DECLARE i INT DEFAULT 0;
WHILE i < n DO
SET return_str =CONCAT(return_str,SUBSTRING(chars_str,FLOOR(1+RAND()*52),1));
SET i = i + 1;
END WHILE;
RETURN return_str;
END $$
创建一个随机产生一个数字的函数rand_num()
DELIMITER $$
CREATE FUNCTION rand_num()
RETURNS INT(5)
BEGIN
DECLARE i INT DEFAULT 0;
SET i = FLOOR(100+RAND()*10);
RETURN i;
END $$
如果要删除这个自定义函数:
drop function rand_num;
创建存储过程
DELIMITER $$
CREATE PROCEDURE insert_crownsize(IN START INT(10),IN max_num INT(10))
BEGIN
DECLARE i INT DEFAULT 0;
SET autocommit = 0;
REPEAT
SET i = i + 1;
INSERT INTO payment (id, `serial`) VALUES (i,rand_string(6));
UNTIL i = max_num
END REPEAT;
COMMIT;
END $$
调用存储过程
DELIMITER ;
CALL insert_crownsize(100001,10);
|