mysql 使用存储过程批量插数据

1
2
3
4
5
6
7
8
9
#创建测试表
DROP TABLE IF EXISTS test.test;
CREATE TABLE test.test(
id int(10) not null auto_increment,
a int(10) not null,
b int(10) not null,
c int(10) not null,
PRIMARY key (`id`)
)ENGINE INNODB DEFAULT CHARSET utf8 COMMENT '测试表';

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#清空数据
TRUNCATE table test.test;
 
#定义存储过程
delimiter //
DROP PROCEDURE IF EXISTS insert_test_val;
##num_limit 要插入数据的数量,rand_limit 最大随机的数值
CREATE PROCEDURE insert_test_val(in num_limit int,in rand_limit int)
BEGIN
 
DECLARE i int default 1;
DECLARE a int default 1;
DECLARE b int default 1;
DECLARE c int default 1;
 
WHILE i<=num_limit do
 
set a = FLOOR(rand()*rand_limit);
set b = FLOOR(rand()*rand_limit);
set c = FLOOR(rand()*rand_limit);
INSERT into test.test values (null,a,b,c);
set i = i + 1;
 
END WHILE;
 
END
//
 
#调用存储过程
call insert_test_val(100000,10);

 

结果:

 

相关链接 http://blog.sina.com.cn/s/blog_9d0b00a4010122wf.html

MySQL 存储过程参数用法 in, out, inout: 

http://www.blogjava.net/nonels/archive/2009/04/22/233324.html

 

posted @   风与叶子  阅读(18782)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
点击右上角即可分享
微信分享提示