Under SQL Server, you can create a table-valued function rather than creating a temporary table, as this is reusable between queries:
-- List all of the dates between startdate and enddate (inclusive)
CREATE FUNCTION [dbo].[DatesBetween] (
@startdate date,
@enddate date
) RETURNS @ret TABLE (Date date) AS BEGIN
DECLARE @dt date, @dtEnd date
SELECT @dt = @startdate, @dtEnd = @enddate
WHILE (@dt <= @dtEnd) BEGIN
INSERT INTO @ret VALUES(@dt)
SET @dt = DATEADD(day, 1, @dt)
END
RETURN
END
This allows everything to be executed inside one query:
SELECT d.Date, COUNT(t.*) AS TotalOnDay
FROM dbo.DatesBetween('2011-01-01', '2011-12-31') d
LEFT JOIN MyTable t ON t.Date = d.Date
GROUP BY d.Date