有时也被称为索引表(index-by tables),可以在PL/SQL中使用,能够模拟数组的非永久表。
用法:
1、 定义一个PL/SQL表类型
2、 声明这种类型的变量。
3、 将记录添加到PL/SQL表中,并采用与引用数组元素大体相同的方式引用他们
语法:p233
特点:只能有一列和一个主健。但是可以有无数的行
举例:
set serveroutput on
declare--定义PL/SQL表
type my_text_table_type is table of varchar2(200)
index by binary_integer;
type my_emp_table_type is table of employees%rowtype
index by binary_integer;
l_text_table my_text_table_type;
l_emp_table my_emp_table_type;
begin
l_text_table(1) :=’donny’;
l_text_table(2) :=’Chen’;
l_emp_table(10).employee_id :=10;
l_emp_table(10).first_name:=’Sean’;
l_emp_table(10).last_name:=’Dillon’;
l_emp_table(10).email :=’donny@hotmail’;
l_emp_table(10).hire_date:=sysdate;
l_emp_table(10).job_id :=’HR’;
l_emp_table(20).employee_id :=20;
l_emp_table(20).first_name:=’Chris’;
l_emp_table(20).last_name:=’Doo’;
l_emp_table(20).email :=’donny@sina’;
l_emp_table(20).hire_date:=sysdate-5;
l_emp_table(20).job_id :=’IT’;
dbms_output.put_line(l_text_table.count);
dbms_output.put_line(l_emp_table.count);
dbms_output.put_line(l_text_table(1));
dbms_output.put_line(l_text_table(2));
dbms_output.put_line(l_emp_table(1).first_name);
dbms_output.put_line(l_emp_table(2).first_name);
end;
/
关于索引算子:
必须是合法的BINARY_INTEGER,介于--2的31次幂~2的31次幂
从PL/SQL表中删除记录:
declare--定义PL/SQL表
type my_text_table_type is table of varchar2(200)
index by binary_integer;
l_text_table my_text_table_type;
l_empty_table my_text_table_type;
begin
l_text_table(10):=’A value’;
l_text_table(20):=’Another value’;
l_text_table(30):=’third value’;
dbms_output.put_line(l_text_table.count);
l_text_table.DELETE(20);
dbms_output.put_line(l_text_table.count);
l_text_table.DELETE;
dbms_output.put_line(l_text_table.count);
l_text_table(15):=’some text’;
l_text_table(25):=’ some more text’;
dbms_output.put_line(l_text_table.count);
l_text_table:=l_empty_table;
dbms_output.put_line(l_text_table.count);
end;
/
FIRST / NEXT / LAST:
我们知道count返回表中行的数量。
FIRST: 返回PL/SQL表中“第一个”或者最小的索引。LAST则是最后一个。
NEXT返回PL/SQL表中当前索引的下一个索引。NEXT有一个参数,即当前的索引。
Conn scott/tiger
Set serveroutput on
Declare
type my_text_table_type is table of varchar2(200)
index by binary_integer;
l_text_table my_text_table_type;
l_index number;
begin
for emp_rec in (select * from emp) loop
l_text_table(emp_rec.empno):=emp_rec.ename;
end loop;
l_index:= l_text_table.first;
loop
exit when l_index is null;
dbms_output.put_line(l_index ’:’ l_text_table(l_index));
l_index :=l_text_table.next(l_index);
end loop;
end;
/
记录:
TYPE <记录名称 IS RECORD(
字段名1 类型;
字段名2 类型;
。。。。。。
);
记录可以拥有任意数目的不同数据类型的列,而PL/SQL表却不是这样的,只能有一列和一个主健。
declare
type location_record_type is record(
street_addressvarchar2(40),
postal_code varchar2(40),
cityvarchar2(30),
state_province varchar2(25),
contry_idchar(2) not null :=’US’
);
l_my_loc location_record_type;
begin
l_my_loc.street_address:=’
Oracle street
’;
l_my_loc.postal_code:=’10010’;
l_my_loc.city:=’Reston’;
l_my_loc.state_province:=’CA’;
dbms_output.put_line(l_my_loc.street_address);
dbms_output.put_line(l_my_loc.postal_code);
dbms_output.put_line(l_my_loc.city);
dbms_output.put_line(l_my_loc.state_province);
end;
/
绑定变量:
declare
type Numtab is table of char(15) index by binary_integer;
type Nametab is table of char(25) index by binary_integer;
vnums Numtab;
vname Nametab;
t1 char(5);
t2 char(5);
t3 char(5);
procedure get_time( t out number) is
begin
select to_char(sysdate,’sssss’) into t from dual;
end;
begin
for j in 1.. 15000 loop
vnums(j) :=j;
vname(j) :=’vendor nmane ‘ to_char(j);
end loop;
get_time(t1);
forall i in 1.. 15000
insert into vender values(vnums(i), vname(i));
get_time(t2);
for i in 1.. 15000 loop
insert into vender values(vnums(i), vname(i));
end loop;
get_time(t3);
dbms_output.put_line(‘Forall loop ’ to_char(t2-t1));
dbms_output.put_line(‘For loop ’ to_char(t3-t2));
end;
/
declare
type Numtab is table of char(15) index by binary_integer;
type Nametab is table of char(25) index by binary_integer;
vnums Numtab;
vname Nametab;
cursor c1 is select * from vender;
begin
open c1;
fetch c1 bulk collect into vnums, vname;
dbms_output.put_line(vnums.count);
close c1;
end;
/
create or replace type add_ty as object(
h_no number(3),
h_street varchar2(20),