-- ef_Vyroba_GenerujRozpadCasu
CREATE FUNCTION dbo.ef_Vyroba_GenerujRozpadCasu (
   @datStart DATETIME=NULL,
   @numOfDays SMALLINT=3,
   @stepMinutes SMALLINT=60
)

/*
RETURNS TABLE
AS
RETURN 
(
   WITH Recursive_CTE AS
   (
       SELECT dbo.hf_TruncDate(ISNULL(@datStart, GETDATE())) AS TimestampUtc
        UNION ALL
       SELECT DATEADD(MINUTE, @stepMinutes, TimestampUtc) 
         FROM Recursive_CTE
        WHERE TimestampUtc < DATEADD(ms, -3, dbo.hf_TruncDate(DATEADD(DAY, @numOfDays, DATEADD(DAY, 1, ISNULL(@datStart, GETDATE())))))
   )
   SELECT *
    FROM Recursive_CTE
)
*/
RETURNS @Result TABLE (TimestampUtc DATETIME)
AS
BEGIN
   DECLARE @currentTimestamp DATETIME = dbo.hf_TruncDate(ISNULL(@datStart, GETDATE()));
   DECLARE @endTimestamp DATETIME = DATEADD(ms, -3, dbo.hf_TruncDate(DATEADD(DAY, @numOfDays, DATEADD(DAY, 1, ISNULL(@datStart, GETDATE())))));

   WHILE @currentTimestamp <= @endTimestamp
   BEGIN
       INSERT INTO @Result (TimestampUtc)
       VALUES (@currentTimestamp);

       SET @currentTimestamp = DATEADD(MINUTE, @stepMinutes, @currentTimestamp);
   END;

   RETURN;
END