Попробуйте, это должно работать
DECLARE @TempWeekDates TABLE (ID INT IDENTITY(1,1), ScheduleDate DATE, WeekNo INT, DayOfWeekUno INT, MonthUno INT, YearUno INT)
DECLARE @Year INT = 2019, @Month INT = 7, @Count INT = 1
;WITH numbers
AS
(
Select 1 as value
UNION ALL
Select value + 1 from numbers
where value + 1 <= Day(EOMONTH(datefromparts(@Year,@Month,1)))
)
INSERT INTO @TempWeekDates (ScheduleDate, MonthUno, YearUno)
SELECT datefromparts(@Year,@Month,numbers.value) AS ScheduleDate, @Month, @Year FROM numbers
WHILE ((SELECT MAX(ID) FROM @TempWeekDates) >= @Count)
BEGIN
UPDATE @TempWeekDates SET DayOfWeekUno = DATEPART(dw,ScheduleDate), WeekNo= (DATEPART(WEEK, ScheduleDate) -
DATEPART(WEEK, DATEADD(MM, DATEDIFF(MM,0,ScheduleDate), 0))+ 1) WHERE ID = @Count
SET @Count = @Count + 1
END
SELECT * FROM @TempWeekDates