phpmyadmin创建mysql的存储过程

通过phpmyadmin ,创建procedure,

用于生成测试数据。

随机的用户名及手机号。

DELIMITER $$
CREATE  PROCEDURE `sp_insert_test_users`(IN `para_count` INT)
BEGIN
    DECLARE p_username varchar(50);
    DECLARE p_countryCallingCode varchar(10) default '86';
    DECLARE p_phone varchar(20);
    DECLARE p_all_phone varchar(20);
    DECLARE p_create_time datetime;    
    DECLARE p_index int default 0;
    DECLARE p_userid int default 0;  
IF para_count > 0 THEN
    SET p_create_time = NOW();
   
    while p_index < para_count do    
        
        select concat(
              substring('ABCDEFGHIJKLMNOPQRSTUVWXYZ', rand()*26 , 1),
              substring('abcdefghijklmnopqrstuvwxyz', rand()*26 , 1),
              substring('abcdefghijklmnopqrstuvwxyz', rand()*26 , 1),
              substring('abcdefghijklmnopqrstuvwxyz', rand()*26 , 1),
              substring('abcdefghijklmnopqrstuvwxyz', rand()*26 , 1),
              substring('abcdefghijklmnopqrstuvwxyz', rand()*26 , 1),
              substring('abcdefghijklmnopqrstuvwxyz', rand()*26 , 1),
              substring('abcdefghijklmnopqrstuvwxyz', rand()*26 , 1)
             ) into p_username;
             
        select concat(
              '139',
              substring('1234567890', rand()* 10 , 1),
              substring('1234567890', rand()* 10 , 1),
              substring('1234567890', rand()* 10 , 1),
              substring('1234567890', rand()* 10 , 1),
              substring('1234567890', rand()* 10 , 1),
              substring('1234567890', rand()* 10 , 1),
              substring('1234567890', rand()* 10 , 1),
              substring('1234567890', rand()* 10 , 1)
             ) into p_phone;
        
        if not exists ( select 1 from users where username = p_username or phone = p_phone ) 
        and length(p_username) = 8 and length(p_phone) = 11
        then 
        
            set p_all_phone = concat(p_countryCallingCode,';',p_phone);
            
            INSERT INTO `users`
            (`username`, `countryCallingCode`, `phone`, `_phone`, 
            `group`,  `created_at`, `updated_at`) 
            VALUES 
            (p_username,p_countryCallingCode,p_phone,p_all_phone,
            'user', p_create_time, p_create_time);
            
             select @@IDENTITY into p_userid;
            
            INSERT INTO `users_test_mock`
            (`userid`,`username`, `countryCallingCode`, `phone`) 
            VALUES 
            (p_userid,p_username,p_countryCallingCode,p_phone);
            
            SET p_index = p_index + 1;
            
        end if;
    
    
    end while;
   
        
END IF;

END$$
DELIMITER ;

 

调用:

call sp_insert_test_users(10);

转载于:https://www.cnblogs.com/freeliver54/p/10457955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值