SqlServer 2008 创建测试数据

2017-10-17 19:19:47来源:cnblogs.com作者:丨渊丨人点击

分享

包含要点: 数据库的循环 、 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 判断

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台