2016-09-02 41 views
3

我要創建一個查詢此步驟:寫有自我更新查詢加入

update t1 
set c3='000' 
from t_loop t1 
join t_loop t2 
    on t1.c2=t2.c1 
where t1.c3 is null 
    and t2.c3 is not null 

while (@@rowcount>0) 
    update t1 
    set c3='000' 
    from t_loop t1 
    join t_loop t2 
     on t1.c2=t2.c1 
    where t1.c3 is null 
     and t2.c3 is not null 

這段代碼檢查,其中列c1已C3價值,然後循環用於匹配每個C2,重視發展C3。

例如:

select * 
into [t_loop] 
from 
(
    select 'v1'c1,'v2'c2,NULL c3 union all 
    select 'v10','v9',NULL union all 
    select 'v2','v3',NULL union all 
    select 'v3','v2','000'union all 
    select 'v4','v2',NULL union all 
    select 'v5',NULL ,NULL union all 
    select 'v6',NULL ,NULL union all 
    select 'v7','v1',NULL union all 
    select 'v8','v7',NULL union all 
    select 'v9',NULL ,NULL union all 
    select 'va','vb',NULL union all 
    select 'vb','vc',NULL union all 
    select 'vc','vb',NULL union all 
    select 'vd',NULL ,NULL union all 
    select 've',NULL ,NULL union all 
    select 'vf','vb','000' 
)t 

,然後將結果是:

c1 c2 c3 
v1 v2 000 
v10 v9 NULL 
v2 v3 000 
v3 v2 000 
v4 v2 000 
v5 NULL NULL 
v6 NULL NULL 
v7 v1 000 
v8 v7 000 
v9 NULL NULL 
va vb NULL 
vb vc NULL 
vc vb NULL 
vd NULL NULL 
ve NULL NULL 
vf vb 000 

我試圖TIEH CTE但我可以做T吧...有人可以幫我嗎?

!!解決了!!

使用後的gofr1

這裏的CTE解釋

--CTE explosion 

--query 1 
    SELECT t1.c1, 
      t1.c2 
    from t_loop t1 
    join t_loop t2 
     on t1.c2=t2.c1 
    where t1.c3 is null 
     and t2.c3 is not null 
    union all 
--query 2 
    SELECT t1.c1, 
      t1.c2 
    from t_loop t1 
    join --cte -> replace cte with the first query (query 1) 
    (
     SELECT t1.c1, 
       t1.c2 
     from t_loop t1 
     join t_loop t2 
      on t1.c2=t2.c1 
     where t1.c3 is null 
      and t2.c3 is not null 
    ) 
    t2 
     on t1.c2=t2.c1 
    where t1.c3 is null 
    union all 
--query 3 
    SELECT t1.c1, 
      t1.c2 
    from t_loop t1 
    join --cte -> replace cte with the second query (query 2) 
    (
     SELECT t1.c1, 
       t1.c2 
     from t_loop t1 
     join 
     (
      SELECT t1.c1, 
        t1.c2 
      from t_loop t1 
      join t_loop t2 
       on t1.c2=t2.c1 
      where t1.c3 is null 
       and t2.c3 is not null 
    ) 
    t2 
     on t1.c2=t2.c1 
    where t1.c3 is null 
    ) t2 
     on t1.c2=t2.c1 
    where t1.c3 is null 
+0

會是怎樣的預期輸出。 – StackUser

+0

你需要達到什麼樣的結果?或最後的結果集是你想要的嗎? – gofr1

回答

1

使用CTE:

;WITH cte AS (
    SELECT t1.c1, 
      t1.c2, 
      '000' as c3 
    from t_loop t1 
    join t_loop t2 
     on t1.c2=t2.c1 
    where t1.c3 is null 
     and t2.c3 is not null 
    UNION ALL 
    SELECT t1.c1, 
      t1.c2, 
      '000' as c3 
    from t_loop t1 
    join cte t2 
     on t1.c2=t2.c1 
    where t1.c3 is null 
     and t2.c3 is not null 
) 


UPDATE t 
SET c3 = c.c3 
FROM [dbo].[t_loop] t 
INNER JOIN cte c 
    ON t.c1 = c.c1 
     and t.c2 = c.c2 

SELECT * 
FROM [dbo].[t_loop] 

輸出爲您提供的相同。

+0

嗨,非常感謝! 我做了同樣的查詢,但我用「t1.c3」而不是「000」作爲c3「。 有什麼區別? – elle0087

+0

我的榮幸!有一個語句't1.c3爲空',所以如果你選擇't1.c3',將會有NULL,並且你需要'000'。 – gofr1

+0

mmmm,再次感謝你,但我很困惑......對我來說,CTE仍然不清楚......我希望早日明白它! – elle0087

0

GO 16將更新16次。

UPDATE [t_loop] 
SET c3 = '000' 
WHERE 
[t_loop].c3 IS NULL AND 
EXISTS 
(
    SELECT 1 FROM dbo.[t_loop] t 
    WHERE 
     t.c1 = [t_loop].c2 AND 
     t.c3 IS NOT NULL 
) 
GO 16 -- SELECT COUNT(*) FROM [t_loop] 

結果:

c1 c2 c3 
---- ---- ---- 
v1 v2 000 
v10 v9 NULL 
v2 v3 000 
v3 v2 000 
v4 v2 000 
v5 NULL NULL 
v6 NULL NULL 
v7 v1 000 
v8 v7 000 
v9 NULL NULL 
va vb NULL 
vb vc NULL 
vc vb NULL 
vd NULL NULL 
ve NULL NULL 
vf vb 000 

其他選項:

DECLARE @Counter INT = 1 
WHILE @Counter < (SELECT COUNT(*) FROM [t_loop]) 
BEGIN 
    UPDATE [t_loop] 
    SET c3 = '000' 
    WHERE 
    [t_loop].c3 IS NULL AND 
    EXISTS 
    (
     SELECT 1 FROM dbo.[t_loop] t 
     WHERE 
      t.c1 = [t_loop].c2 AND 
      t.c3 IS NOT NULL 
    ) 

    SET @Counter += 1 
END