oracle pl/sql集合

来源:互联网 发布:mysql创建索引 编辑:程序博客网 时间:2024/04/29 14:49

1 :各种情况下接受数据库查询结果:
(1)单行单列——-标量
(2)单行多列———自定义type或rowtype
(3)多行单列(多列<索引表>)———–集合(索引表,嵌套表,varray)

2:索引表
**type type_name is table of elment_type
index by key_type ;
identifier type_name ;**

 (1) **索引下标为 binary_integer 或 pls_integer**

declare
type ename_type is table of emp.empname%type
index by binary_integer;
ename ename_type;
begin
select empname into ename(1) from emp where empno =&no;
select empname into ename(2) from emp where empno =&po;
dbms_output.put_line(‘姓名1’ || ename(1) );
dbms_output.put_line(‘姓名2’ || ename(2) );
end;

declare
type ename_type is table of emp%rowtype
index by binary_integer;
ename ename_type;
begin
select * into ename(1) from emp where empno =&no;
select * into ename(2) from emp where empno =&po;
dbms_output.put_line(ename(1).empname || ‘.’|| ename(1).sal);
dbms_output.put_line(ename(2).empname || ‘.’|| ename(2).sal);
end;

declare
type city_type is table of varchar2(10)
index by varchar2(10);
city_table city_type;
begin
city_table(‘上海’):= ‘shanghai’;
city_table(‘北京’):= ‘beijin’;
city_table(‘西安’):= ‘xian’;
dbms_output.put_line(city_table.first);
dbms_output.put_line(city_table.last);
end;

(2)嵌套表
下标从1开始, 没有边界,使用前必须对其初始化
**type type_name is table of elment_type ;
identifier type_name ;**
declare
type name_type is table of varchar2(10);
name_table name_type;
begin
name_table:=name_type(‘刘攀峰’,’尹公峰’,’同勃舟’,’王玉军’);
dbms_output.put_line(name_table.count);
for i in 1..name_table.count loop
dbms_output.put_line(name_table(i));
end loop;
end;

declare
type empname_type is table of emp.empname%type;
empname_table empname_type;
begin
empname_table := empname_type (”,”,”);
select empname into empname_table(1) from emp where empno =001;
dbms_output.put_line(empname_table(1));
end;

(3)变长数组
下标从1开始, 有最大边界,使用前必须对其初始化
**type type_name is varray(limit_size) of elment_type
identifier type_name ;**

declare
type sal_type is varray(20) of number;
v_varray sal_type;
begin
v_varray := sal_type(20,30,40);
dbms_output.put_line(v_varray(1) || ’ ’ || v_varray(2) || ’ ’ || v_varray(3) || ’ ’ );
end;

(4 ) 在表列中使用嵌套表或变长数组

create type type_name is varray (table ) of elment_type;
/
create table kk (
id number,
name varchar2(10),
xxx type_name
);

0 0