2010-03-12 37 views
6

任何人都可以告訴我如何批量插入數據從遊標到PL/SQL中的臨時表?我有一個過程,它的一個參數存儲結果集,這個結果集將被插入到另一個存儲過程的臨時表中。如何批量插入數據從遊標到PL/SQL中的臨時表

這是我的示例代碼。

CREATE OR REPLACE PROCEDURE get_account_list 
(
type_id in account_type.account_type_id%type, 
acc_list out sys_refcursor 
) 
is 
begin 
    open acc_list for 
    select account_id, account_name, balance 
    from account 
    where account_type_id = type_id; 
end get_account_list; 

CREATE OR REPLACE PROCEDURE proc1 
(
    ... 
) 
is 
    accounts sys_refcursor; 
begin 
    get_account_list(1, accounts); 

    --How to bulk insert data in accounts to a temporary table? 


end proc1; 

在SQL Server中,我可以按照以下

CREATE PROCEDURE get_account_list  
    type_id int 
as 
    select account_id, account_name, balance 
    from account 
    where account_type_id = type_id; 



CREATE PROCEDURE proc1 
(
    ... 
) 
as 
    ... 

    insert into #tmp_data(account_id, account_name, balance) 
    exec get_account_list 1 

代碼寫我怎麼能寫類似於SQL Server中的代碼?謝謝。

回答

6

你可以在REF CURSOR使用批量操作:

SQL> CREATE GLOBAL TEMPORARY TABLE gt (ID NUMBER); 

Table crÚÚe. 

SQL> DECLARE 
    2  l_refcursor SYS_REFCURSOR; 
    3  TYPE tab_number IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; 
    4  l_data tab_number; 
    5 BEGIN 
    6  OPEN l_refcursor FOR 
    7  SELECT ROWNUM FROM dual CONNECT BY LEVEL <= 1e6; 
    8  LOOP 
    9  FETCH l_refcursor BULK COLLECT 
10   INTO l_data LIMIT 100; 
11 
12  FORALL i IN 1..l_data.count 
13   INSERT INTO gt VALUES (l_data(i)); 
14 
15  EXIT WHEN l_refcursor%NOTFOUND; 
16 
17  END LOOP; 
18  CLOSE l_refcursor; 
19 END; 
20/

ProcÚdure PL/SQL terminÚe avec succÞs. 
雖然

Oracle 10g中已經實現了這種優化定期循環,所以你可能看不到一個簡單太大起色LOOP ... INSERT。

2

如何

procedure insert_rec(in_type_id in number) is 
    begin 
    insert into temp_table 
    select account_id, account_name, balance 
    from account 
    where account_type_id = in_type_id; 
end insert_rec; 
+0

這並不容易。我的代碼只是一個示例。過程get_account_list在裏面有很多計算,並最終得到一個結果集,並通過參數acc_list傳遞給其他過程。謝謝。 – 2010-03-12 06:41:18