CREATE FUNCTION dbo.DateRange (@StartDate DATETIME, @EndDate DATETIME, @PacketNumber INT) RETURNS @SelectedRange TABLE (IndividualDate DATETIME, PacketNumber INT) AS 
BEGIN
  WITH cteRange (DateRange, PacketNumber) AS (
    SELECT @StartDate, @PacketNumber
    UNION ALL
    SELECT DATEADD(dd, 1, DateRange), @PacketNumber
    FROM cteRange
    WHERE DateRange <= DATEADD(dd, -1, @EndDate))
    INSERT INTO @SelectedRange (IndividualDate, PacketNumber)
    SELECT DateRange, PacketNumber
    FROM cteRange
    OPTION (MAXRECURSION 0);
    RETURN
END