Hey!!! I liked the way you worked out the problem. I have a little comment for some of the code. You are using while loops in some of the code that you can replace with pure SQL code.
The example is when creating the table with the weeks. The program cycles and inserts data into a table. I thinks this algorithm would help you in two ways. It's faster and easier to read.
DECLARE @FromDate DATETIME, @ToDate DATETIME
SELECT @FromDate = '20091228', @ToDate = '20111231'
-- Number of days between the from and to dates
DECLARE @Diff INT
SELECT @Diff = DATEDIFF(DAY, @FromDate, @ToDate)
SELECT TOP (@Diff / 7 + 1)
-- ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) gives us the week number
-- First day of week gets calculated with offset 0
DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) * 7 + 0, @FromDate) AS DateStart,
-- Last day of the week gets calculates with offset 6
DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) * 7 + 6, @FromDate) AS DateEnd
FROM
Sys.columns C1, Sys.columns C2
As you can see I base it in ROW_NUMBER() function so, it will just run on SQL Server 2005 or above.
Regards.
Ariel from Argentina.