Bulk Collect的用法

本文介绍Oracle数据库中的批量操作技巧,包括使用BULK COLLECT进行高效的数据检索与处理,通过FOR循环遍历集合进行批量插入、更新及删除操作,并展示了如何在RETURNING子句中结合BULK COLLECT进行数据备份。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--select col1,col2 bulk collect into ..
Declare
  Type tv_object_name Is Table Of ou.Object_Name%Type Index By Binary_Integer;
  var_object_name tv_object_name;
  Type ti_object_id Is Table Of ou.Object_Id%Type Index By Binary_Integer; 
  i_object_id ti_object_id;
  Begin
    Select object_name,object_id Bulk Collect Into var_object_name,i_object_id
      From ou Where Rownum <=100;
    Forall i In 1..i_object_id.count
      Insert Into h_ou Select * From ou Where object_name=var_object_name(i)
                                     And object_id = i_object_id(i);
    Commit;
  End;
--Fetch cursorname bulk collect into ..
Declare
  Type tv_rowid Is Table Of Varchar(20) Index By Binary_Integer;
  var_rowid tv_rowid;

  Cursor v_cur(pv_object_type Varchar2) Is Select Rowid From ou Where object_type=pv_object_type;
  v_object_type Varchar2(20);
  Begin
    v_object_type := 'TABLE PARTITION';
    Open v_cur(v_object_type);
    Loop
      Fetch v_cur Bulk Collect Into var_rowid Limit 1000;
      Forall i In 1..var_rowid.count
        Insert Into h_ou
          Select * From ou Where Rowid = var_rowid(i);
      Forall i In 1..var_rowid.count
        Delete From ou Where Rowid = var_rowid(i);
      Commit;
      Exit When v_cur%Notfound Or v_cur%Notfound Is Null;
    End Loop;
    Close v_cur;
  End;
--在returning into中使用bulk collect
Create Table ou_list As Select object_name,object_id From Dba_Objects Where 1=2;
Declare
  Type tv_object_name Is Table Of ou.Object_Name%Type Index By Binary_Integer;
  var_object_name tv_object_name;
  Type ti_object_id Is Table Of ou.Object_Id%Type Index By Binary_Integer; 
  i_object_id ti_object_id;
Begin
  Delete From ou
    Returning object_name,object_id Bulk Collect Into var_object_name, i_object_id;
  Forall i In 1..i_object_id.count
    Insert Into ou_list Values (var_object_name(i),i_object_id(i));
  Commit;
End;

 
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值