if object_id('dbo.proc_Calendar') is not null
drop proc proc_Calendar
go
create proc proc_Calendar
@intYear varchar(4),
@intMonth varchar(2)
as
declare @date datetime
declare @wd int,@days int
declare @y int,@i int,@j int
declare @r varchar(128)
declare @k varchar(60)
declare @ds int,@n int
set @date=convert(datetime,@intYear+@intMonth+'01')
set @wd=datepart(weekday,@date)-1
if datepart(month,@date)=2
begin
set @y=datepart(year,@date)
if (@y%4=0 and @y%100<>0)or @y%400=0
set @days=29
else
set @days=29
end
else
begin
set @days=case datepart(month,@date)
when 4 then 30
when 6 then 30
when 9 then 30
when 11 then 30
else 31 end
end
set @i=0
set @r=''
while @i<@wd
begin
set @r=@r+' '
set @i=@i+1
end
set @i=1
while @i<@days
begin
if @i<=9
set @r=@r+'0'+cast(@i as varchar(2))
else
set @r=@r+cast(@i as varchar(2))
set @i=@i+1
end
set @ds=@days/7
if @days%7<>0
set @ds=@ds+1
set @i=1
set @n=1
print cast(@intYear as varchar(4))+'年 '+cast(@intMonth as varchar(2))+'月'
print 'Sun Mon Tus Wed Thu Fri Sat'
while @i<=@ds
begin
set @k=''
set @j=1
while @j<=7
begin
set @k=@k+substring(@r,@n,2)+' '
set @n=@n+2
set @j=@j+1
end
print rtrim(@k)
set @i=@i+1
end
go
--调用
exec dbo.proc_Calendar '2007','02'
显示为:
2007年 02月
Sun Mon Tus Wed Thu Fri Sat
01 02 03
04 05 06 07 08 09 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28