通过指定的起始时间,创建该时间段内以年、月、日为时间段的临时表
ALTER PROCEDURE [dbo].[YOUR_SP_Name]
-- Add the parameters for the stored procedure here
@StartTime DATETIME
,@EndTime DATETIME
AS
BEGIN
-- 将开始时间赋值给临时变量
DECLARE @TempTime DATETIME
SET @TempTime = @StartTime
--创建字段为年、月、日的临时表#TMP
CREATE TABLE #TMP
(
[Year] INT NOT NULL,
[Month] INT NOT NULL,
[Day] INT NOT NULL
)
--将给时间段内的日期插入临时表#TMP
WHILE(@TempTime BETWEEN @StartTime AND @EndTime)
BEGIN
INSERT INTO #TMP VALUES(
DATEPART(YEAR,@TempTime)
,DATEPART(MONTH,@TempTime)
,DATEPART(DAY,@TempTime)
)
SET @TempTime = DATEADD(DAY,1,@TempTime)
END
--下面就可以对你的临时表做操作啦
SELECT * FROM #TMP
--删除临时表#TMP
DROP TABLE #TMP
END