1 pl/sql 集合 处理单列多行数据库,使用的类型为标量类型
1) 索引表
type ename_table_type is table of emp.ename%type
index by binary_integer;
ename_table ename_table_type;
begin
select ename into ename_table(-1) from emp
where empno=&no;
dbms_output.put_line('雇员名:'||ename_table(-1));
end;
set serveroutput no
declare
type area_table_type is table of number
index by varchar2(10);
rea_table area_table_type;
begin
area_table('beijing'):=1;
area_table('shanghai'):=2;
area_table('guangzhou'):=3;
dbms_output.put_line(area_table.first);
dbms_output.put_line(area_table.last);
end;
2) 嵌套表
索引表类型不能作为累得数据类型使用,但是嵌套表可以作为表类的数据类型使用。
当使用嵌套表元素时,必须先用其构造方法初始化其嵌套表:
a 在pl/sql 块中使用嵌套表
declare
type ename_table_type is table of emp.ename%type;
ename_table ename_table_type;
begin
ename_table:=eanme_table_type('2','2','3');
select ename into ename table(2) from emp where empno=&no;
dbms_ouput.put_line(ename_table(2));
end;
b 在表中使用嵌套表
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;
-- 为嵌套表插入数据
insert into employee values(2,'scott',200,phone_type('2222','333333'));
--检索嵌套表累得数据
set erveroutput on
declare
phone_table phone_type;
begin
select phone into phone_table
from employee where id=1;
for i in 1..phone_table.count loop
dbms_output.put_line(phone_table(i));
end loop;
end;
-- 更新嵌套表列的数据
delcare
phone_table phone_type:=('44444','555555');
begin
update employee set phone=phone_table
where id=1;
end;
3) 变长数组
在使用varray 时必须指定最大个数,和数据类型,在使用其元素时必须进行初始化
type ename_table_type is varray(20) of emp.ename%type;
ename_table ename_table_type:=ename_table_type('1','2');
-- 在快中使用varray
declare
type ename_table_type is varray(20) of emp.ename%type;
ename_table ename_table_type:=ename_table_type('mary');
begin
select ename into ename_table(1) form emp
where empno=$no;
end;
--在表列中使用varray
create type phone type is varray(20) of varchar2(20);
create table employee(
id number(4),name varchar2(10),
sal number(6,2),phone phone_type);
3)记录表
记录表结合了记录和集合的优点
declare
type emp_table_type is table of emp%rowtype
index by binary_integer;
emp_table emp_table_type;
begin
select * from into emp_table(1) from emp
where empno=&no;
dbms_output.put_line(emp_table(1).ename);
end;
4)多维集合
1 多级varray
declare
--define 一维集合
type al_array_type is varray(10) of int;
--定义二维集合
type nal_varray_type is varray(10) of a1_varray_type;
--初始化二维集合
nvl nal_varray_type:=nal_varray_type(
a1_varray_type(1,2),
a1_varray_type(2,3)
)
beign
for i in 1..nal_varray_type.count loop
for j in 1..a1_array_type.count loop
dbms_out.putline(nvl(i)(j));
end loop;
end loop;
end;
2 使用多级嵌套表
table a1_table_type is table of int;
table nvl_table_type is table of a1_table_type;
nvl nvl_table_type:=nvl_table_type(
a1_table_type(1,2),
a1_table_type(2,3)
);
2 集合方法
1) exist
if ename_table.exists(1) then
ename_table(1):='scott';
2) count 返回当前集合变量中的元素总个数
ename_table.count
3) limit 返回集合元素的最大个数 只有varray 有
4)first and last
ename_table.first
ename_table.last
5) prior 和next
ename_table.prior(5); --返回元素5的前一个
ename_table.next(5); -- 后一个
6) extend
使用于varray 和 嵌套表。
extend add a null value
extend (n) add n null value
extend (n,i)add n i value
declare
type ename_table_type is varray(20) of varchar2(20);
ename_table ename_table_type;
begin
ename_table:=ename_table_type('mary');
ename_table.extend(5,1);
dbms_output.put_line(ename_table.count);
end;
7) trim
trim remove one element from the tail of the collection.
trim(n) remove n element from the tail of the colleciton.
8)delete
delete: delete all the elements
delete(n) :delete the nth elements
delete(m,n): delete the elements from m to n
3 集合赋值
1)将一个集合的数据赋值给另一个集合.clear the destination collectins and set the original
collection
delcare
type name_varray_type is varray(4) of varchar2(10);
name_array1 name_varray_type;
name_array2 name_varray_type;
begin
name_array1:=name_varray_type('scott','smith');
name_array2:=name_array_type('a','b','c');
name_array1:=name_array2;
end;
type name_array1_type is varray(4) of varchar2(10);
type name_array2_type is varray(4) of varchar2(10);
name_array1 name_array1_type;
name_array2 name_array2_type;
具有相同的数据类型,单具有不同的集合类型不能构赋值
2) 给集合赋城null 值
可以使用delete 或 trim
也可以使用 空集合赋给目表集合
type name_varray_type is varray(4) of varchar2(10);
name_array name_varray_type;
name_empty name_varray_type;
name_array:=name_varray_type('1','2');
name_array:=name_empty;
3) 使用集合操作赋和比较集合都是10g 的内容,p176 先略过。
4 批量绑定
执行单词sql 操作能传递所有集合元素的数据。
1 forall 语句
用于insert update 和delete操作。在oracle9i 中forall 语句必须具有连续的元素
1) using forall on insert
declare
type id_table_type is table of number(6)
index by binary_integer;
type name_table_type is table of varchar2(2)
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 demo values(id_table(i),name_table(i));
end;
2)using forall on using update
forall i in 1..id_table.count
upate demo set name:=name_table(i)
where id:=id_table(i);
3)using forall on using delete
forall i in 1..id_table.count
delete from demo where id:=id_table(i);
4) using forall on part of the collection
for i in1..10 loop
id_table(i):=i;
name_table(i):="name"||to_char(i);
end loop;
forall i in 8..10 l
insert into demo values(id_table(i),name_table(i));
2 bulk collect
is fit for select into ,fetch into and dml clause
1) using bulk collect
declares
type emp_table_type is table of emp%rowtype
index by binary_integer;
emp_table emp_table_type;
begin
select * bulk collect into emp_table
from emp where deptno=&no;
for i in 1..emp_tablee.count loop
dbms_output.put_line(emp_table(i).ename);
end loop;
2) 在dml 的返回字句使用bulk collect 字句
declare
type ename_table_type is table of emp.ename%type;
ename_table ename_table_type;
begin
deletee from emp where deptno=&no
returning ename bulk_collect into ename_table;
for i in 1..ename_table.count loop
dbms_output.put(ename_table(i));
end loop;
end;
end;
end;