ALTER PROCEDURE [p_make_arraymonth](
@dtbegin varchar(10),
@dtend varchar(10)
)
AS
begin
----
declare @d1 datetime
declare @d2 datetime
set @d1=CAST(@dtbegin as datetime)
set @d1=CAST((DATENAME(yyyy,@d1)+'.'+right((month(@d1)+100),2)+'.01') as datetime)
set @d2=CAST(@dtend as datetime)
set @d2=CAST((DATENAME(yyyy,@d2)+'.'+right((month(@d2)+100),2)+'.02') as datetime)
create table #t_dt(dt varchar(7))
insert into #t_dt(dt) values(DATENAME(yyyy,@d1)+'.'+right((month(@d1)+100),2))
while (@d2>@d1)
begin
insert into #t_dt(dt) values(DATENAME(yyyy,@d2)+'.'+right((month(@d2)+100),2))
set @d2=DATEADD (mm,-1,@d2)
continue
end
select distinct dt as 月份 from #t_dt order by dt asc
-----
end
循环你不会?