包含重點: 資料庫的循環、 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 建立測試資料的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!