sql 循环 ,随机数,循环插入一年数据

--循环插入一年的时间
DROP TABLE #temp
CREATE TABLE #temp (date DATETIME);
 
DECLARE @year INT
SET @year=2019;
WITH a1 AS
(
SELECT CAST(RTRIM(@year)+'-01-01' AS DATETIME) date
UNION ALL
SELECT DATEADD(dd,1,date) FROM a1 WHERE DATEPART(yy,DATEADD(dd,1,date))=@year
)
INSERT #temp
SELECT date FROM a1 
EXCEPT 
SELECT date FROM #temp

OPTION(MAXRECURSION 0)


--sql 一定范围随机数
CAST((rand()*300+ 100) as dec(10,0)) 


--sql 循环
declare @carID DECIMAL
set @carID=0 
while @carID<24
begin


--INSERT  INTO EM_RegionEnergyConsumption VALUES(10,CONVERT(varchar(100), GETDATE(), 23),@Result1 )
set @carID=@carID+1
END

 

posted @ 2019-03-23 15:04  Provedl  阅读(1182)  评论(0编辑  收藏  举报