--按月份生成报表
ALTER proc [dbo].[Mag_Sell_MonthReport]
@SumYear int
as
----生成动态汇总SQL字符串
declare @sql nvarchar(max)
declare @sqlstr nvarchar(max)
set @sql = ''
select @sql = @sql + ',' + QUOTENAME(Sellyear)
from (select cast(Allmonth AS varchar(12))+'月' as Sellyear
from [dbo].[Report_Month]() ) a
set @sql = stuff(@sql, 1, 1, '')
set @sqlstr ='select * from(select stocktype as ''库存\月份'',
cast(month(SellDate) AS varchar(12))+''月'' as Sellyear, procount from V_Mag_Sell_Report where year(SellDate)='+cast(@SumYear AS varchar(12));
exec(@sqlstr+')DATA PIVOT(SUM(procount)FOR Sellyear IN(' + @sql + '))P')