2012-09-04 26 views
0

我有一個關於使用遊標的問題。同一個循環中的多個遊標

Table t1 having "t1c1" and "t1c2" columns. 

Table t2 having "t2c2" and "t2c2" columns. 

Table t3 having "t3c2","t3c2","t3c3","t3c4" columns. 

Two cursors "cur1" and "cur2". 

我寫代碼,我需要使用光標「CUR1」

實施例的循環將值插入T3:

DECLARE 

CURSOR cur1 IS 
SELECT t1c1 FROM t1; 

CURSOR cur2 IS 
SELECT t2c1 FROM t2; 

BEGIN 

FOR f1 IN cur1 LOOP 

EXIT WHEN cur1%NOTFOUND; 

INSERT INTO TABLE t3 
(
    SELECT f1.t1c1,t2.t2c2,'hello' FROM t2; 
); 

END LOOP; 

從我已插入第一三列以上的代碼表t3。

我想知道如何將cur2(遊標值)插入表t3的第4列。

+0

問題標記爲[tag:oracle]和[tag:mysql]:你在用什麼?爲什麼不在表中使用'INSERT ... SELECT'([MySQL](http://dev.mysql.com/doc/en/insert-select.html)文檔)? – eggyal

回答

0
insert into t3 
select t1.t1c1, t2.t2c2, 'Hello' 
from t1, 
    t2 

我真的很奇怪你沒有提到t1和t3之間的任何聯繫,所以你得到了cartezian。

+0

爲什麼要把t3和t1連接起來.... – kattashri