2014-11-24 23 views
1

我需要填充用數字(它是該表的ID列),然後陣列遍歷所述陣列調用存儲過程。選號成數組,然後遍歷它們

我有麻煩搞清楚如何聲明一個數組中,我不知道,直到運行時,然後填充它的大小。

目前,我有以下幾點:

declare 
    type idArray is table of number index by pls_integer; 
    theIdArray idArray; 
begin 

    select id into theIdArray from table_name_here where report_id = 3449; 
end; 

這不是工作,但我不知道爲什麼。然後我還需要遍歷我的數組數組並調用一個存儲過程。像下面這樣:

for i in theIdArray.FIRST..theIdArray.LAST LOOP 
    stored_proc_here(i); 
END LOOP; 

有人可以告訴我一些如何做到這一點。到目前爲止,我所得到的例子都來自於我不熟悉的例子。因爲你使用條款into

+1

嘗試用'bulk collect into'替換'into'子句' – mikron 2014-11-24 13:26:20

回答

2

你的代碼沒有。代替填充集合使用bulk collect into

declare 
    type idArray is table of number index by pls_integer; 
    theIdArray idArray; 
begin 

    select id bulk collect into theIdArray from table_name_here where report_id = 3449; 
end; 
+0

我在select後添加了for循環,並且得到了這個錯誤PLS-00487:對變量'I''的無效引用。我不確定爲什麼它也會在錯誤中使用'I',因爲它是小寫字母,但我知道這不是錯誤的原因。 – Justin 2014-11-24 13:33:35

+0

對不起,錯誤是因爲'(i.id)'。如果你使用'theIdArray.FIRST..theIdArray.LAST',那麼你引用該對象。在迭代集合使用'i'代替'i.id' – Aramillo 2014-11-24 13:40:36

+0

只是一個側面說明:如果'theIdArray'可以是空的,你應該在1重複使用'因爲我.. theIdArray.COUNT'和使用'參考theIdArray(I) '而不是使用'first' /'last'。這將允許循環跳過,而不是拋出索引超出界限的錯誤。 – Andy 2014-11-24 13:44:42

1

如果你正在做的是循環也可能是一樣容易使用遊標循環。

declare 
    cursor Ids is 
     select ID from table_name_here where report_id = 3449; 
... 
for Report in Ids loop 
    stored_proc_here(Report.ID); 
end loop; 

您不必擔心顯式打開,關閉,取出,分配或取消分配。所有這些都是由循環處理的。