SQLServer 循环1百万插入测试数据

1,首先创建student表

create table student
 (
 sno int primary key ,
 sname VARCHAR(200)
 )
 

 

2,--向数据库中插入100万条随机姓名记录用于测试(sqlserver2005)

 
DECLARE @LN VARCHAR(300),@MN VARCHAR(200),@FN VARCHAR(200) 
DECLARE @LN_N INT,@MN_N INT,@FN_N INT 
SET @LN='李王张刘陈杨黄赵周吴徐孙朱马胡郭林何高梁郑罗宋谢唐韩曹许邓萧冯曾程蔡彭潘袁于董余苏叶吕魏蒋田杜丁沈姜范江傅钟卢汪戴崔任陆廖姚方金邱夏谭韦贾邹石熊孟秦阎薛侯雷白龙段郝孔邵史毛常万顾赖武康贺严尹钱施牛洪龚'
SET @MN='德绍宗邦裕傅家積善昌世贻维孝友继绪定呈祥大正启仕执必定仲元魁家生先泽远永盛在人为任伐风树秀文光谨潭棰' 
SET @FN='丽云峰磊亮宏红洪量良梁良粮靓七旗奇琪谋牟弭米密祢磊类蕾肋庆情清青兴幸星刑' 
SET @LN_N=LEN(@LN) 
SET @MN_N=LEN(@MN) 
SET @FN_N=LEN(@FN) 
DECLARE @TMP VARCHAR(1000),@I INT SET @I=100 
WHILE @I<1000000 BEGIN    
 SET @TMP=CAST(SUBSTRING(@LN,CAST(RAND()*@LN_N AS INT),1) AS VARCHAR)     
 SET @TMP=@TMP+CAST(SUBSTRING(@MN,CAST(RAND()*@MN_N AS INT),1) AS VARCHAR)     
 SET @TMP=@TMP+CAST(SUBSTRING(@FN,CAST(RAND()*@FN_N AS INT),1) AS VARCHAR)     
 INSERT INTO student(sno,sname)VALUES('2005'+@I,@TMP)     
SET @I=@I+1 
end
posted @ 2015-05-25 10:49  专注、坚持  阅读(910)  评论(0编辑  收藏  举报