2012-12-02 66 views
0

我有以下Oracle過程(我已經採取了一些細節,並且使問題更加普遍:從INSERT返回多個值和插入兩到另一個表

create or replace 
procedure Insert_Row (foo IN VARCHAR2, 
         buzz in VARCHAR2, 
         t_in MyType) 
is 
l_cur_id number; 
begin 
    insert into table1 (foo,buzz) 
returning bar into l_cur_id; 
BEGIN 
FOR i IN 1..t_in.count LOOP 
insert into table2 (bar, something) 
    values(l_cur_id,t_in(i)); 
    commit; 
END LOOP; 
end; 
end; 

它所做的就是插入一行到table1,會從剛剛插入table1該行的ID,並用它來插入table2

下面是我使用上述類型:

create or replace 
TYPE MyType AS VARRAY(200) OF VARCHAR2(50); 

我的問題:如何將buzz插入table2?即第二個值。我認爲一定很簡單,因爲我在插入之前已經有了buzz的值。

非常感謝。

+0

發佈後直接解決 - 典型!將發佈解決方案... – ale

回答

0

好吧,我都加做的是這樣的:

變化來自:

BEGIN 
FOR i IN 1..t_in.count LOOP 
insert into table2 (bar, something) 
    values(l_cur_id,t_in(i)); 
    commit; 
END LOOP; 

這樣:

BEGIN 
FOR i IN 1..t_in.count LOOP 
insert into table2 (bar, something,buzz) 
    values(l_cur_id,t_in(i),buzz); 
    commit; 
END LOOP; 

那麼明顯:-S。

+1

建議不要在存儲過程中進行提交,將其留給調用應用程序。尤其不要在循環中提交。 –

+0

謝謝尼古拉斯,我會實施這個改變。 +1。 – ale