设为首页 收藏本站
查看: 341|回复: 0

[经验分享] oracle 复合数据类型(批量绑定)

[复制链接]

尚未签到

发表于 2016-8-9 06:42:29 | 显示全部楼层 |阅读模式
  批量绑定是oracle9i新增加特性,是指执行单次SQL操作能传递所有集合元素的数据,通过批量绑定可以极大的加快数据处理速度,提高应用程序的性能,批量绑定是使用bulk collect子句和forall语法完成,其他bulk  collect子句用于取得批量数据,该子句只能用于select语句、fetch语句和DML返回子句,而forall语句只适用于批发批量的DML操作。
一、forall语句
当要在PL/SQL应用程序中执行批量insert、update、delete操作时,可以使用forall语句,在oracle9i之前,当使用forall语句时,必须具有连续的元素,而从oracle9i开始,通过使用新增的indices of子句和values of子句,可以使用不连续的集合元素,注意for语句是循环语句,而forall可不是循环语句,从oracle10g开始,forall语句有是三种执行方法,如下所示;
1、语法一:
   forall  index  in lower_bound..upper_bound
       sql_statement;
如上所示:index是隐含定义的整数变量(将作为集合元素下标被引用);lower_bound和upper_bound分别是集合元素的上、下界。
2、语法二:
   forall index in indies of collection
     [between lower_bound and upper_bound]
如上所示:indices of子句用于指定只取得对应中的collection集合元素下标的index值。
3、语法三:
forall index in values of index_collection
    sql_statement;
如上所示:其中values of子句用于指定index值从集合变量index_collection中取得,注意Oracle9i只能使用第一种方法,以下通过示例说明:
(1)、在insert语句上使用批量绑定
   当使用批量绑定为数据库表插入数据时,首先需要给集合元素赋值,然后使用forall语句执行批量绑定插入操作,示例如下:
  







declare
type id_table_type is  table of number(6)
index by binary_integer;
type name_table_type is  table of varchar2(10)
index by binary_integer;
id_table id_table_type;
name_table name_table_type;
begin
for i in 1..10 loop
id_table(i):=i;
name_table(i):='name'||to_char(i);
end loop;
forall i in 1..id_table.count
insert into demo values(id_table(i),name_table(i));
end;
  
(2)、在update语句上使用批量绑定
   当使用批量绑定为更新数据时,首先需要给集合元素赋值,然后使用forall语句执行批量绑定修改操作,示例如下:







declare
type id_table_type is table of number(6) index by binary_integer;
type name_table_type is table of varchar2(10) index by binary_integer;
id_table id_table_type;
name_table name_table_type;
begin
for i in 1..10 loop
id_table(i):=i;
name_table(i):='yanglin'|| to_char(i);
end loop;
forall i in 1..id_table.count
update demo set name=name_table(i) where id=id_table(i);
end;
  
(3)、在delete语句上使用批量绑定
   当使用批量绑定删除数据时,首先需要给集合元素赋值,然后使用forall语句执行批量绑定删除操作,示例如下:







declare
type id_table_type is table of number(6) index by binary_integer;
id_table id_table_type;
begin
for i in 1..10 loop
id_table(i):=i;
end loop;
forall i in 1..id_table.count
delete from  demo where id=id_table(i);
end;

  
(4)、在forall语句上使用部分集合元素
   当使用forall语句进行批量操作时,即可以使用集合的所有元素,也可以使用集合的部分元素,示例如下:







declare
type id_table_type is table of number(6) index by binary_integer;
id_table id_table_type;
begin
for i in 11..20 loop
id_table(i):=i;
end loop;
forall i in 15..20
delete from  demo where id=id_table(i);
end;

  
(4)、在forall语句上使用indices of子句
   indices of子句是oracle10g新增加的特征,该子句用于跳转null集合元素,示例如下:







declare
type id_table_type is table of number(6);
id_table id_table_type;
begin
id_table:=id_table_type(11,null,12,null,13,null);
forall i in indices of id_table
delete from  demo where id=id_table(i);
end;

  
二、bulk collect子句
bulk collect用于取得批量数据,它只适用于select into语句、fetch into语句和DML返回子句,通过使用该子句可以将批量数据存放到PL/SQL集合变量中,其语法如下:
... bulk collect into collection_name[,collection_name].....
如上所示:collection_name用于指定集合变量名
1、在select into语句使用bulk collect语句,示例如下:







declare
type temp_table_type is table of cip_temps%rowtype index by binary_integer;
temp_table temp_table_type;
begin
select * bulk collect into temp_table from cip_temps where id=10;
for i in 1..temp_table.count loop
dbms_output.put_line(temp_table(i).name ||':'|| temp_table(i).address||':'||temp_table(i).age);
end loop;
end;

  
2、在DML返回子句中使用bulk collect语句,示例如下:
   执行DML操作时会改变数据库数据,为了取得DML操作所改变的数据,可以使用returning子句,为了取得DML所作用的多行数据,需要使用bulk collect子句,示例如下:


declare   
type temp_table_type is table of cip_temps%rowtype index by binary_integer;   
temp_table temp_table_type;   
begin   
delete from cip_temps where id=1  
returning name,age,address,id bulk collect into temp_table;   
dbms_output.put_line('信息'|| temp_table.count);   
for i in 1..temp_table.count loop   
dbms_output.put_line(temp_table(i).name ||':'|| temp_table(i).age||':'||temp_table(i).address||':'||temp_table(i).id);   
end loop;   
end;  

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-254977-1-1.html 上篇帖子: Oracle添加修改删除表字段 下篇帖子: oracle 复合数据类型(pl/sql记录)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表