有人能告诉我如何将数据从引用游标批量插入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中的代码?感谢。
答案 0 :(得分:6)
你可以在REF CURSOR上使用BULK操作:
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。
答案 1 :(得分: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;