使用MySql Workbench创建存储过程,在表中插入100条数据
程序员文章站
2022-04-08 09:09:20
...
创建存储过程:
CREATE DEFINER="root"@"localhost" PROCEDURE "sd_insert_procedure"()/*创建存储过程*/
BEGIN
/*定义变量*/
declare max_id int;
declare max_sd_id int;
declare age int;
declare mingcheng varchar(20);
declare sex varchar(20);
declare province_name varchar(200);
/*给变量赋值*/
select id into max_id FROM student1_table order by id desc limit 1;/*student1_table表为已创建的表,可看上一篇文章*/
select sd_number into max_sd_id FROM student1_table order by sd_number desc limit 1;
set @n =1;
set @SURNAME = '王李张刘陈杨黄赵吴周徐孙马朱胡郭何高林罗郑梁谢宋唐位许韩冯邓曹彭曾萧田董潘袁于蒋蔡余杜叶程苏魏吕丁任沈姚卢姜崔钟谭陆汪范金石廖贾夏韦傅方白邹孟熊秦邱江尹薛阎段雷侯龙史陶黎贺顾毛郝龚邵万钱严覃武戴莫孔向汤';/*赋值字符串,用于后续的人员名称中的姓氏*/
set @NAME = '丹举义之乐书乾云亦从代以伟佑俊修健傲儿元光兰冬冰冷凌凝凡凯初力勤千卉半华南博又友同向君听和哲嘉国坚城夏夜天奇奥如妙子存季孤宇安宛宸寒寻尔尧山岚峻巧平幼康建开弘强彤彦彬彭心忆志念怀怜恨惜慕成擎敏文新旋旭昊明易昕映春昱晋晓晗晟景晴智曼朋朗杰松枫柏柔柳格桃梦楷槐正水沛波泽洁洋济浦浩海涛润涵渊源溥濮瀚灵灿炎烟烨然煊煜熙熠玉珊珍理琪琴瑜瑞瑶瑾璞痴皓盼真睿碧磊祥祺秉程立竹笑紫绍经绿群翠翰致航良芙芷苍苑若茂荣莲菡菱萱蓉蓝蕊蕾薇蝶觅访诚语谷豪赋超越轩辉达远邃醉金鑫锦问雁雅雨雪霖霜露青靖静风飞香驰骞高鸿鹏鹤黎';/*姓名*/
set @sd_sex = '男女';/*性别*/
/*n从1到101循环100次,插入100条数据*/
while (@n<101)
do
set age = cast(ceiling(rand() * 100) As double);
select concat(substr(@surname,floor(rand()*length(@surname)/3+1),1),
substr(@NAME,floor(rand()*length(@NAME)/3+1),1),
substr(@NAME,floor(rand()*length(@NAME)/3+1),1)) INTO mingcheng;
select substr(@worker_sex,floor(rand()*length(@worker_sex)/3+1),1) into sex;
select provincecol into province_name from province ORDER BY RAND() LIMIT 1 ;/*province表为已创建的表,里面存储了各省的名称*/
insert into student1_table (id,sd_number,sd_name,sd_age,sd_sex,sd_address)
values (max_id+@n,max_sd_id+@n,mingcheng,age,sex,province_name);
/*插入数据*/
set @n=@n+1;/*n自增1*/
end while;
END
运行的结果: