Contains key points: database loop, insert select sentence pattern, random number (rand() function), absolute value (abs() function)
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 判断
The above is the detailed content of How to create test data in SqlServer 2008. For more information, please follow other related articles on the PHP Chinese website!