oracle bulk collect into 實現批量插入
- 語法:
fetch
遊標bulk collect into
目標[limit num]
*** 不加limit
默認取遊標中全部數據插入到目標中,如果加上limit,則限制數據量
- 示例
表結構:
declare
type tb_type is table of sales%rowtype; --聲明一個表類型 tb_type
cursor mycursor is select productname from sales; --定義一個遊標
my_tb tb_type; --定義一個表類型變量
type name_list_type is table of sales.productname%type; --定義一個表類型
name_list name_list_type; --定義一個表類型變量(只有一列)
begin
open mycursor;
loop
fetch mycursor bulk collect into name_list limit 3; --用 bulk collect into 實現批量插入, 加 limit num 可以限定插入行數
for it in 1..name_list.count loop
dbms_output.put_line(name_list(it)); --這裏訪問數組使用 數組(索引)
end loop;
dbms_output.put_line('-----------');
exit when mycursor%notfound;
end loop;
close mycursor;
end;
- 結果
籃球
足球
羽毛球
·-----------
乒乓球
泰迪熊
·-----------