I have just completed something similar.I built a dynamic table depending on the months between the two dates.set @SQLCmd = 'create table dbo.DynamTab( [a] [int] IDENTITY(1,1) NOT NULL, [b] [nvarchar](255) NULL, [c] [nvarchar](50) ' while @i <= @FieldCount begin set @SQLCmd = @SQLCmd + '[' + convert(varchar,datepart(mm,dateadd(mm,@i,@FirstMonth))) + '/' + convert(varchar,datepart(yy,dateadd(mm,@i,@FirstMonth))) + '] decimal(19,2) NULL CONSTRAINT [DF_DynamTab_' + convert(varchar,datepart(mm,dateadd(mm,@i,@FirstMonth))) + '/' + convert(varchar,datepart(yy,dateadd(mm,@i,@FirstMonth))) + '] DEFAULT ((0))' + case when @i = @FieldCount then ')' else ',' end set @i=@i+1 end exec (@SQLCmd)
Then I populated the table with the values I required for a,b and c.Then update the dynamic fields by using something like:while @i <= @FieldCount begin set @CurrentMonth = (select case when @i = 0 then datepart(mm,@FirstMonth) else datepart(mm,dateadd(mm,@i,@FirstMonth)) end) set @CurrentYear = (select case when @i = 0 then datepart(yy,@FirstMonth) else datepart(yy,dateadd(mm,@i,@FirstMonth)) end) set @SQLCmd = 'Update dbo.DynamTab set [' + convert(varchar,@CurrentMonth) + '/' + convert(varchar,@CurrentYear) + '] = ' <yourvaluehere> endexec @SQLCmd
Obviously I have simpliffied this and you may have to add parts on depending on how you calculate the retvals.