This nested while loop is only successfully executing the lowest level loop. It refuses to add 1 to @wKey even though I tell it to SET @wKey = @wKey + 1.... What am I doing wrong here? Does SQL not allow nested loops?
DECLARE @fMinKey INT;
DECLARE @fMaxKey INT;
DECLARE @wMaxKey INT;
DECLARE @wKey INT;
DECLARE @wDate date;
DECLARE @fcStart DATE;
SET @fMinKey = (select min([fcKey]) from dbo.fact_Fc);
SET @fMaxKey = (select max([fcKey]) from dw.fact_Fc);
SET @wMaxKey = (select max([WellKey]) from dw.fact_Fc);
SET @wKey = 1;
SET @wDate =
(select min([fapDate]) from dbo.dim_W where [Key] = @wKey);
SET @fcStart =
(select min([Date]) from dw.fact_Fc where [wKey] = @wKey);
WHILE (@fMinKey <= @fMaxKey)
BEGIN
WHILE (@wKey <= @wMaxKey)
BEGIN
WHILE (@wDate < @fcStart)
BEGIN
INSERT INTO dw.fact_FcTemp2 ([wKey], [Date], [pAmount], [fcKey], [AddedDate])
VALUES (@wKey, @wDate, 0, @fMinKey, CURRENT_TIMESTAMP)
SET @wDate = dateadd(DAY, 1, @wDate)
END
SET @wKey = @wKey + 1
END
SET @fMinKey = @fMinKey + 1
END
The resulting table is only showing [wKey] = 1, but it should have rows for multiple different wKeys