--创建临时表CREATE TABLE #L(ID int IDENTITY(1,1) NOT NULL,Data NVARCHAR(50)PRimary key(ID))--@CurrentYear INT 当前年,@CurrentMonth INT 当前月,--@StartYear INT 起始年,@StartMonth INT起始月DECLARE @CurrentYear INT,@CurrentMonth INT,@StartYear INT,@StartMonth INT SET @StartYear=2012 SET @StartMonth=1 SET @CurrentYear=DATEPART(YEAR, GETDATE()) SET @CurrentMonth=DATEPART(MONTH,GETDATE()) DECLARE @dt DATETIME,@dts NVARCHAR(50) WHILE @StartYear<@CurrentYear+1BEGINIF(@StartYear<>@CurrentYear)BEGIN WHILE @StartMonth<13BEGIN--获取最后每一月的最后一天--SELECT @dt=dateadd(day,-1,dateadd(month,1,rtrim(@StartYear)+'-'+rtrim(@StartMonth)+'-01'))--获取最后每一月的最后一天的最后一秒SELECT @dt=dateadd(second,-1,dateadd(day,-1,rtrim(@StartYear)+'-'+rtrim(@StartMonth)+'-01'))PRINT @dtSET @dts=CONVERT(NVARCHAR(20),@dt,120)INSERT INTO #L VALUES(@dts)SET @StartMonth=@StartMonth+1ENDENDELSE BEGINWHILE @StartMonth<@CurrentMonth+1BEGINSELECT @dt=dateadd(second,-1,dateadd(day,-1,rtrim(@StartYear)+'-'+rtrim(@StartMonth)+'-01'))PRINT @dtSET @dts=CONVERT(NVARCHAR(20),@dt,120)INSERT INTO #L VALUES(@dts)SET @StartMonth=@StartMonth+1END END SET @StartYear=@StartYear+1SET @StartMonth=1 END DECLARE @time varchar(50) DECLARE My_Cursor CURSOR --定义游标FOR (SELECT Data FROM #L) --查出需要的集合放到游标中OPEN My_Cursor; --打开游标FETCH NEXT FROM My_Cursor INTO @time; --读取第一行数据WHILE @@FETCH_STATUS = 0 BEGIN PRINT @time; --打印数据 INSERT INTO datetimess VALUES (@time) FETCH NEXT FROM My_Cursor INTO @time; --读取下一行数据 ENDCLOSE My_Cursor; --关闭游标DEALLOCATE My_Cursor; --释放游标GO--删除临时表DROP TABLE #L
新闻热点
疑难解答