CREATE FUNCTION dbo.TimeSlot (@AStartDate DATETIME, @AEndDate DATETIME, @AIntervalInSeconds INT, @AWareNumber INT) RETURNS @SelectedRange TABLE (StartDate DATETIME, EndDate DATETIME, WareNumber INT) AS 
BEGIN
  WITH ten AS
  (
    SELECT n FROM (Values(1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) t(n)
  ), millions AS
  (
    SELECT TOP(DATEDIFF(SECOND, @AStartDate, @AEndDate) / @AIntervalInSeconds)
    ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS n
    FROM ten c
    CROSS JOIN ten c1
    CROSS JOIN ten c2
    CROSS JOIN ten c3
    CROSS JOIN ten c4
    CROSS JOIN ten c5
    CROSS JOIN ten c6
    ORDER BY n
  )
  INSERT INTO @SelectedRange (StartDate, EndDate, WareNumber)
  SELECT DATEADD(SECOND, @AIntervalInSeconds * (n-1), @AStartDate) AS StartDate, DATEADD(SECOND, @AIntervalInSeconds * n, @AStartDate) AS EndDate, @AWareNumber
  FROM millions
  RETURN
END