`
q272156430
  • 浏览: 269821 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

oracle 复合数据类型(批量绑定)2

阅读更多

一、PL/SQL集合
    为了处理单列多行,可以使用PL/SQL集合进行处理。PL/SQL集合类似于高级语言数组的一种复合数据类型、集合类型包括索引表(PL/SQL表)、嵌套表(Netsed Table)、变长数组(VARRAY)等三种类型。
1、索引表
  索引表也成为PL/SQL表,它是oracle早期用来处理PL/SQL数组的数据类型,注意,高级语言数组的元素个数是有限制的,并且下标不能为负数,而索引表的元素个数没有限制,并且下标可以为负数,注意索引表只能作为PL/SQL复合数据类型使用,而不能作为表列的数据类型使用,定义索引表的语法如下:
type type_name is table of element_name
[not null]index by key_type;
identifier type_name;
如图所示:type_name用于指定自定义数据类型的名称(is table..index表示索引表),element_type用于指定索引表元素的数据类型,not null表示不允许应用null元素,key_type用于指定索引表元素下标的数据类型(binary_integer、pls_integer或varchar2);identifier用于定义索引表变量,注意oracle9i之前,索引表下标只允许用的数据类型binary_integer和pls_integer;而从oracle9i开始不仅可以为binary_integer和pls_integer,而且还可以为varchar2类型,
示例一、在索引表中使用binary_integer和pls_integer

 declare
 type temp_table_type is table of cip_temps.NAME%type
 index by binary_integer;
 temp_table temp_table_type;
 begin
    select name into temp_table(-1) from cip_temps where id=6;
    dbms_output.put_line(temp_table(-1));
 end;


示例二、在索引中使用varchar2

declare
 type temp_table_type is table of number
 index by varchar2(10);
 temp_table temp_table_type;
begin
temp_table('北京'):=1;
temp_table('上海'):=2;
temp_table('河北'):=3;
dbms_output.put_line(temp_table.first);
dbms_output.put_line(temp_table.last);
end;

 

2、嵌套表


  嵌套表也是一种用来处理PL/SQL数组的数据类型,高级语言数组元素下标是用0或1开始,并且元素个数有界限,而嵌套表下标是从1开始的,并且元素个数没有界限,另外高级语言数组元素值是顺序的,而嵌套表的数组元素值是可以稀疏的,注意索引表类型不能做为表列的数据类型使用,但嵌套表类型可以做为表列的数据类型使用, 语法如下:
type type_name is table of element_type;
identifier type_name;
如上所示,type_name用于指定嵌套表名称,element_type用于指定嵌套表元素的数据类型,identifier用于定义嵌套表变量,注意:当使用嵌套表时,必须首先使用构造方法初始化嵌套表,示例如下:
delcare
type temp_table_type is table of emp.name%type;
temp_table temp_table_type:=temp_table_type('A','A');
使用嵌套表的说明方法
(1)在PL/SQL快中使用嵌套表
   必须要初始化嵌套表变量,然后才能在PL/SQL块中引用嵌套表,示例如下:

declare
 type temp_table_type is table of cip_temps.NAME%type;
 temp_table temp_table_type;
begin
temp_table:=temp_table_type('mary','123','123');
select name into temp_table(3) from cip_temps where id=6;
dbms_output.put_line(temp_table(3));
end;


(2)、在表列中使用嵌套表
  嵌套表类型不仅可以再PL/SQL块中直接引用,也可以作为表列的数据类型使用,但如果在表列中使用嵌套表类型,必须首先使用create type命令穿件类型,注意,当使用嵌套表类型做为表列的数据类型,必须要为嵌套表列指定专门的存储表,示例如下:

create type phone_type is table of varchar2(20);
/
create table employee(
id number(4),
name varchar2(10),
sal number(6,2),
phone phone_type
)nested table  phone store as phone_table;


示例一、在PL/SQL块中嵌套表列插入数据
当定义嵌套表类型时,oracle自动为该类型生成相应的构造方法,当为嵌套表列插入数据时,需要使用嵌套表的构造方法,示例如下:

declare
 begin
insert into employee values(1,'1111',5,phone_type('aaaa','3333333'));
end;


示例二、在PL/SQL块中检索嵌套表列的数据
     当在在PL/SQL块中检索嵌套表列的数据时,需要定义嵌套表类型的变量接收其数据,示例如下:

declare
temp_phone phone_type;
 begin
select phone into temp_phone from employee where id=1;
for i in 1..temp_phone.count loop
dbms_output.put_line(temp_phone(i));
end loop;
end;


示例三、在PL/SQL块中更新嵌套表列的数据
  当在PL/SQL块中更新嵌套表列的数据时,首先需要定义嵌套表变量,并使用构造方法初始化该变量,然后才能执行部门使用update语句更新,示例如下:

declare
temp_phone phone_type:=phone_type('aa','bb');
 begin
update employee set phone=temp_phone where id=1;
end;


3、变长数组
其元素下标以1开始,并且元素下标最大是有限制的,定义array语法如下:
type  type_name is vrray(limit_size) of element_type[not null];
identifier type_name;
如上所示:type_name类型名称,limit_size用于指定下标最大值,element_type用于指定元素数据类型,identifer用于定义vrray变量,当使用vrray元素时,必须使用构造方法初始化array元素,示例如下:
declare
type temp_varray_type is vrray(20) of temp.name%type;
temp_varray temp_varray_type:=temp_varray_type('a');
关于用法同嵌套表
二、PL/SQL记录表
PL/SQL变量用于处理单行单列数据,PL/SQL记录用于处理单行多列数据,PL/SQL集合用于处理多列单行数据,为了在PL/SQL快中处理多行多列数据,开发人员可以使用PL/SQL记录表处理多行多列数据,示例如下:

declare   
type temp_table_type is table of cip_temps%rowtype   
 index by binary_integer;   
 temp_table temp_table_type;   
 begin   
  select * into temp_table(1) from cip_temps where id=6;   
  for i in 1..temp_table.count loop   
    dbms_output.put_line(temp_table(i).name||':'||temp_table(i).address);   
  end loop;   
end;  
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics