目的:写一个存储过程,往数据库中插入几百条数据,作为识别码给别人用(这里我觉得和验证码的功能相似)
BEGIN DECLARE count INT(10); DECLARE newAgentCode VARCHAR(10); DECLARE oldAgentCode VARCHAR(5) ; DECLARE randcode VARCHAR(50); DECLARE flag int(10); set flag = 0; set count = 0; while count <= insertCount do SET randcode = REPLACE(UUID(),'-',''); #生成一个32位随机字符串 SET oldAgentCode = LEFT(randcode,5); #截取随机字符串的前5位 select UPPER(oldAgentCode) INTO oldAgentCode; #转大写 select CONCAT('LD',oldAgentCode) INTO newAgentCode; #拼接字符串 SELECT newAgentCode; #如果有重复的就不添加此条记录 select count(*) into flag from ludimb_agentCode where agentCode = newAgentCode; IF flag <1 THEN INSERT INTO ludimb_agentCode (agentCode,createTimes,codeStatus) VALUES (newAgentCode,NOW(),10); end if; set count = count+1; end while; END
以上就是存储过程的代码,我这里是直接在mysql中调用存储过程,也有很多在mybatis后台代码中去调用
以下是插入200条数据:
-- 调用存储过程生成代理人编码 CALL generate_agentcode(200);