Create function [dbo].[FUN_GenerateTime] ( @begin_date datetime, -- 起始时间 @end_date datetime -- 结束时间 ) returns @t table(Weekday CHAR(6),Monthday datetime) as begin with maco as ( select datename(dw,@begin_date) AS Weekday,@begin_date AS Monthday union all select datename(dw,Monthday+1),Monthday+1 from maco where Monthday+1 <=@end_date ) insert into @t select * from maco option(maxrecursion 0); return end GO
调用方法:
SELECT * FROM FUN_GenerateTime('2017-01-01','2017-01-10')
返回结果: