日期:2014-05-17 浏览次数:20607 次
--插入截至到昨天的连续随机日期到表t DECLARE @start_date datetime; DECLARE @insert_date datetime; DECLARE @today datetime = getdate(); SET @start_date = CAST ( (CONVERT(varchar(7),GETDATE(),120)+'-01') AS datetime); SET @start_date = DATEADD(hour,13,@start_date); SET @insert_date = @start_date SET @insert_date = @insert_date + rand() * 1.0/24 --当下一条要插入的数据小于当前日期执行 WHILE DATEPART(day,@insert_date) < DATEPART(day,@today) BEGIN INSERT INTO t VALUES ( @insert_date,'有' ); --准备15:00-16:00的数据 SET @insert_date =CAST( CONVERT(varchar(10),@insert_date,120) as datetime); SET @insert_date = DATEADD(HOUR,15,@insert_date); SET @insert_date = @insert_date + rand() * 1.0/24 INSERT INTO t VALUES ( @insert_date,'无' ); --准备第二天的数据 SET @insert_date = DATEadd(day,1,CAST( CONVERT(varchar(10),@insert_date,120) as datetime)); SET @insert_date = DATEADD(HOUR,13,@insert_date); SET @insert_date = @insert_date + rand() * 1.0/24 END GO