2010-11-08 61 views
1

我要運行依賴於彼此SQL Server的問題存儲過程循環

select a from a (result has multiple rows) 
select b from b (result has multiple rows) 

的用於在查詢的每個行多個查詢我需要運行這個

select c from c where c.a=a amd c.b=b 

insert c into d 

有點像:

select a from a (result has multiple rows) 
select b from b (result has multiple rows 
    for a in a 
     for b in b 
      select c from c where c.a=a amd c.b=b 
      insert c,a,b into d 

這是否可以做爲存儲過程?

謝謝

+0

表一個具有在結果多行等爲b – Birby 2010-11-08 16:15:28

+0

最終的結果應該是這樣的 表d: C,A,B – Birby 2010-11-08 16:15:55

回答

4

存儲過程?當然。射擊,它甚至有可能在一個聲明:

INSERT INTO d (c) 
    SELECT c FROM c WHERE c.a IN (SELECT a FROM a) AND c.b IN (SELECT b FROM b) 

你可以甚至可能做一個更好的加入一點點,而是寫,我需要知道更多有關表如何關聯。

3

我敢肯定,你可以寫這個查詢沒有SQL使用任何循環。

1
INSERT 
INTO d 
SELECT c.* 
FROM a 
CROSS JOIN 
     b 
JOIN с 
ON  c.a = a.id 
     AND c.b = b.id