开发的过程中,很多时候我们需要插入百万数据来测试功能和性能,今天我来教大家最简单的插入方法
-----------------1、新建表---------------------
CREATE TABLE [student] (
[ID] [int] IDENTITY (1, 1) NOT NULL,
[sid] [varchar] (20) NOT NULL,[age] [int] NOT NULL,
[name] [nvarchar] (50) NOT NULL
)
-----------------2、插入百万数据---------------------
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=1
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(sid,age,name)VALUES('2018-'+convert(varchar,@I),cast(floor(rand() * 50+18) as int),@TMP)SET @I=@I+1
end
-----------------统计---------------------
select COUNT(ID) from student
-----------------查看---------------------
select top 100 * from student
-----------------删除表---------------------
drop table student