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 $$
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; //function后面加要删除的自定义函数名
DELIMITER $$
CREATE PROCEDURE insert_crownsize(IN START INT(10),IN max_num INT(10))
BEGIN
DECLARE i INT DEFAULT 0;
#set autocommit = 0 把 autocommit设置为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 ; //切换为;结束sql语句
CALL insert_crownsize(100001,10);//调用自定义存储过程,这里一次提交为10条
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/m0_53157173/article/details/120598234
内容来源于网络,如有侵权,请联系作者删除!