0
此嵌套while循環只能成功執行最低級別的循環。它拒絕給@wKey添加1,即使我告訴它SET @wKey = @wKey + 1 ....我在這裏做錯了什麼? SQL不允許嵌套循環嗎?這個SQL WHILE LOOP有什麼問題?
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
所得表僅表示[WKEY] = 1,但它應當具有多個不同wKeys
,因爲它是嵌套的。 SET在第一個WHILE循環結束之後。換句話說,在第一個WHILE循環完成之後,我想更改@wKey並重新啓動原始循環。 – james5