mysql 使用存儲過程批量插數據


#創建測試表
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 '測試表';

 

#清空數據
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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM