Modified to accommodate the OP's desire for the result to be obtained in @table.
DECLARE @year INT
,@startDate DATE
SET @year = 2010
SET @startDate = CAST(@year AS VARCHAR) + '-01-01'
DECLARE @DateTable TABLE
(
[Date] DATE ,[Col1] INT ,[Col2] INT ,[Col3] INT ,[Col4] INT
,[Col5] INT ,[Col6] INT ,[Col7] INT ,[Col8] INT ,[Col9] INT
)
;
WITH cte
AS ( SELECT @StartDate [Date]
UNION ALL
SELECT DATEADD(DAY, 1, [Date])
FROM [cte]
WHERE [Date] < DATEADD(year, 1, @StartDate)
)
INSERT INTO @DateTable
(
[Date] ,[Col1] ,[Col2] ,[Col3] ,[Col4] ,[Col5]
,[Col6] ,[Col7] ,[Col8] ,[Col9] )
SELECT [Date] ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0
FROM [cte] OPTION (MAXRECURSION 366)
If you are going to use this often,
consider creating Date Tableor at least aTally Table
source
share