其实很简单的一个问题,用SQL server的交叉表就可以了

解决方案 »

  1.   

    试一试把StatMonth@sum改为StatMonth.[@sum]
      

  2.   

    create  proc pcStatMonth
    (
    @tempmonth int,
    @tempyear int,
    @max int
    )
    as
    insert into salemonth (P_Name,sumcell,I_IndentDate)
    select P_Name,sum(L_Value) as sumcell,day(I_IndentDate) as I_IndentDate
    from IndentList,Indent,Product
    where month(I_IndentDate)=@tempmonth
    and year(I_IndentDate)=@tempyear
    and L_I_ID=I_ID
    and L_Product=P_ID
    group by I_IndentDate,P_Name
    order by I_IndentDate ascinsert into StatMonth (P_Name)
    select DISTINCT P_Name from ProductDECLARE @sum NVARECHAR(20)
    DECLARE @Counter INT
    SET @Counter=1While (@Counter<=@max)
    BEGIN
      set @sum='sum' + counter
      exec('UPDATE StatMonth 
      SET StatMonth.'+@sum+'=StatMonth.'+@sum+'+salemonth.sumcell,
          StatMonth.total=StatMonth.total+salemonth.sumcell
          FROM StatMonth,salmonth
          WHERE StatMonth.P_Name=salemonth.P_Name
          AND salemonth.I_IndentDate=@counter')
    END
      

  3.   

    up flowing(流水)SQL SERVER 联机丛书中搜索“交叉数据报表”你可以看到示例