包含要点: 数据库的循环 、 insert select 句式 、 随机数(rand()函数)、绝对值(abs()函数)
declare @starttime varchar(50)
declare @endtime varchar(50)
declare @randomvalue float
set @starttime='2017-09-15 00:15:00.000'
set @endtime='2017-10-20 00:00:00.000'
-- set @randomvalue=floor(rand()*10)
-- -- select convert(varchar(50), dateadd(mi,15,@starttime),121)
--select [tagid],[value]-@randomvalue,[startvalue]-@randomvalue,convert(varchar(50),dateadd(mi,15,[starttime]),121),[endvalue]-@randomvalue,convert(varchar(50),dateadd(mi,15,[endtime]),121)
--from [dbo].[dimtagminutedata] where tagid in (1,
-- 2,3,4,5,6,
-- 7,8,9,10,11,
-- 12,13,14,15,16,
-- 17,18,19,20
-- )
--and starttime=@starttime and endtime=convert(varchar(50),dateadd(mi,15,@starttime),121)
while @starttime<=@endtime
begin
set @randomvalue=floor(rand()*10)
if @starttime<=@endtime
begin
insert into dimtagminutedata ([tagid],[value],[startvalue]
,[starttime]
,[endvalue]
,[endtime])
select [tagid] ,
abs([value]-@randomvalue) ,
abs([startvalue]-@randomvalue),
convert(varchar(50),dateadd(mi,15,[starttime]),121),
abs([endvalue]-@randomvalue),
convert(varchar(50),dateadd(mi,15,[endtime]),121)
from [dbo].[dimtagminutedata] where tagid in (1,
2,3,4,5,6,
7,8,9,10,11,
12,13,14,15,16,
17,18,19,20
)
and starttime=@starttime and endtime=convert(varchar(50),dateadd(mi,15,@starttime),121)
end
else
begin
print 'stop'
end
set @starttime= convert(varchar(50), dateadd(mi,15,@starttime),121)
end
-- 随机数
select floor(rand()*100)当然 循环的时候 可以不用 if else 判断
以上就是sqlserver 2008 创建测试数据的方法的详细内容。
