Sqlserver新建随机测试数据

USE Test --使用数据库Test(如果没有则需要新建一个)

----1.新建一个users表
create table users(
uId int primary key identity(1,1),
uName varchar(50) null,
uSex bit null default(1),
uPwd varchar(50) null,
uEmail varchar(100) null,
uPwdfindone nvarchar(50) null,
uPwdfindtwo nvarchar(50) null,
uPwdfindthree nvarchar(50) null,
uPwdfindanswer1 nvarchar(50) null,
uPwdfindanswer2 nvarchar(50) null,
uPwdfindanswer3 nvarchar(50) null,
uCity varchar(50) null,
uBirthyear int null,
uBirthmonth int null,
uBirthday int null,
uScore int null,
uRegistertime datetime null default(getdate()),
uRegisterIP varchar(50) null
)
GO

----2.循环向users表插入随机数据
declare @s int --循环起点
declare @n int --年
declare @y int --月
declare @r int --日
declare @h int --时
declare @f int --分
declare @m int --秒
declare @mm int --毫秒
declare @t varchar(30) --注册时间
declare @address varchar(10)--地址
declare @email varchar(20) --邮箱
declare @xb int --性别
declare @xm varchar(20) --姓名
declare @ma varchar(20) --密码
declare @mq1 varchar(20) --密码问题1
declare @mq2 varchar(20) --密码问题2
declare @mq3 varchar(20) --密码问题3
declare @mqa1 varchar(20) --密码答案1
declare @mqa2 varchar(20) --密码答案2
declare @mqa3 varchar(20) --密码答案3
declare @bn int --出生年份
declare @by int --出生月
declare @br int --出生日
declare @fs int --分数
declare @ip1 int --ip地址第1段
declare @ip2 int --ip地址第2段
declare @ip3 int --ip地址第3段
declare @ip4 int --ip地址第4段
declare @ip varchar(50) --ip地址
set @s=1 --设置循环起点
while(@s<1000000) --设置循环终点
begin
declare @id varchar(10)
set @id=CAST(@s as varchar(10))
begin --注册时间
set @n=1990+abs(checksum(newid()))%(2014-1990+1)
set @y=1+abs(checksum(newid()))%(12-1+1)
set @r=1+abs(checksum(newid()))%(31-1+1)
if(@y=2)--当前月份是2月
begin
if((@n%4=0 and @n%100 <>0) or @n%400=0)--闰年判断
set @r=1+abs(checksum(newid()))%(29-1+1)
else
set @r=1+abs(checksum(newid()))%(28-1+1)
end
else
begin
if(@y%4=0 or @y%6=0 or @y%9=0 or @y%11=0)--4、6、9、11月最多30天
set @r=1+abs(checksum(newid()))%(30-1+1)
end
declare @yy varchar(5)
set @yy=CAST(@y as varchar(5))
if(@y<10)
set @yy='0'+CAST(@y as varchar(5))
declare @rr varchar(5)
set @rr=CAST(@r as varchar(5))
if(@r<10)
set @rr='0'+CAST(@r as varchar(5))
set @h=1+abs(checksum(newid()))%(23-1+1)
set @f=1+abs(checksum(newid()))%(59-1+1)
declare @ff varchar(5)--分,用于格式化分钟
set @ff=CAST(@f as varchar(5))
if(@f<10)
set @ff='0'+CAST(@f as varchar(5))
set @m=1+abs(checksum(newid()))%(59-1+1)
declare @miao varchar(5)--秒,用于格式化秒钟
set @miao=CAST(@m as varchar(5))
if(@m<10)
set @miao='0'+CAST(@m as varchar(5))
set @mm=1+abs(checksum(newid()))%(999-1+1)
set @t=CAST(@n as varchar(5))+'-'+@yy+'-'+@rr+' '+CAST(@h as varchar(5))+':'+@ff+':'+@miao+':'+CAST(@mm as varchar(5))
declare @time datetime
set @time=convert(datetime,@t)
end
begin --城市及邮箱
if(@s%2=0) 
begin
set @xb=0
set @email=@id+'@qq.com'
set @address='Guangzhou'
end
else 
begin	
set @xb=1	
set @email=@id+'@msn.com'
set @address='Berlin'
end
if(@s%11=0)
begin
set @email=@id+'@nas.com'
set @address='Strasbourg'
end
if(@s%22=0)
begin
set @email=@id+'@skyweb.com'
set @address='Campinas'
end
if(@s%33=0)
begin
set @email=@id+'@gly.com'
set @address='Xiamen'
end
if(@s%14=0)
begin
set @email=@id+'@bl.com'
set @address='Dalian'
end
if(@s%15=0)
begin
set @email=@id+'@hpjx.com'
set @address='Hangzhou'
end
if(@s%16=0)
begin
set @email=@id+'@rgm.com'
set @address='Wuhan'
end
if(@s%27=0)
begin
set @email=@id+'@tf.com'
set @address='Chengdu'
end
if(@s%58=0)
begin
set @email=@id+'@fj.com'
set @address='Lijiang'
end
if(@s%39=0)
begin
set @email=@id+'@px.com'
set @address='Lyon'
end
end
begin --姓名、性别、密码问题、密码答案等
set @xm='uname'+@id
set @ma='upwd'+@id
set @mq1='upwdfindone'+@id
set @mq2='upwdfindtwo'+@id
set @mq3='upwdfindthree'+@id
set @mqa1='upwd1answer'+@id
set @mqa2='upwd2answer'+@id
set @mqa3='upwd3answer'+@id
set @bn=1980+abs(checksum(newid()))%(1995-1980+1)
set @by=1+abs(checksum(newid()))%(12-1+1)
set @br=1+abs(checksum(newid()))%(31-1+1)
if(@by=2)--当前月份是2月
begin
if((@bn%4=0 and @bn%100 <>0) or @bn%400=0)--闰年判断
set @br=1+abs(checksum(newid()))%(29-1+1)
else
set @br=1+abs(checksum(newid()))%(28-1+1)
end
else
begin
if(@by%4=0 or @by%6=0 or @by%9=0 or @by%11=0)--4、6、9、11月最多30天
set @br=1+abs(checksum(newid()))%(30-1+1)
end
set @fs=11+abs(checksum(newid()))%(150-11+1)
end
begin --ip地址
set @ip1=1+abs(checksum(newid()))%(254-1+1)
set @ip2=1+abs(checksum(newid()))%(254-1+1)
set @ip3=1+abs(checksum(newid()))%(254-1+1)
set @ip4=1+abs(checksum(newid()))%(254-1+1)
set @ip=CAST(@ip1 as varchar(5))+'.'+CAST(@ip2 as varchar(5))+'.'+CAST(@ip3 as varchar(5))+'.'+CAST(@ip4 as varchar(5))
end
begin --执行sql语句
insert into users(uName,uSex,uPwd,uEmail,uPwdfindone,uPwdfindtwo,uPwdfindthree,uPwdfindanswer1,uPwdfindanswer2,
uPwdfindanswer3,uCity,uBirthyear,uBirthmonth,uBirthday,uScore,uRegistertime,uRegisterIP)
values(
@xm,@xb,@ma,@email,@mq1,@mq2,@mq3,@mqa1,@mqa2,@mqa3,@address,@bn,@by,@br,@fs,@t,@ip
)
end
set @s+=1	
end


----3.检查数据新增是否执行成功
--select * from users

  

转载于:https://www.cnblogs.com/programsky/p/7883973.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值