集合:
集合:存放一组数据类型相同的数据(多条数据),由下标和值组成
一个下标对就一条数据,分类:索引表,嵌套表和变长数组
集合中的属性和方法:
first:取集合中第1个元素的下标
last:取集合中最后一个元素的下标
count:取集合中的元素个数
limit:取集合中最多能存放的元素个数
next(下标):取当前下标对应的元素的下一个元素的下标
prior(下标):取当前下标对应元素的上一个元素的下标
extend(n[,ind]):扩展集合中的元素,n表示扩展的个数,ind表示集合中的元素下标
delete():删除集合中的元素
(1)索引表
索引表:是plsql中的一个集合类型,只能在plsql代码块中使用,由下标和值组成
下标可以是字符串,也可以是数字(整数pls_integer/binary_integer)
索引表的类型定义语法:
type 类型名称 is table of 元素的数据类型 index by 下标数据类型; --类型
变量声名:
变量名 索引表类型名称;
索引表的使用:
变量名(下标) --取一个索引表中的值,按下标取
变量名(下标):=值 --给索引表添加一个元素
declare
–定义了一个存放字符串类型的索引表,它的下标是字符串类型
type ctype is table of varchar2(50) index by varchar2(30);
–声名一个索引表变量
c ctype;
begin
c(‘a’):=‘SMITH’; --添加一个元素
c(‘b’):=‘KING’;
c(‘c’):=‘SCOTT’;
c(‘d’):=‘tom’;
–打印索引表第1个元素的下标
dbms_output.put_line(‘第1个元素的下标first:’||c.first);
–打印索引表的最后一个元素的下标
dbms_output.put_line(‘最后一个元素的下标last:’||c.last);
–打印索引表中的元素个数
dbms_output.put_line(‘元素个数count:’||c.count);
–next方法:取下一个元素的下标,参数是元素下标
dbms_output.put_line(‘取下一个元素的下标next(’‘b’’):’||c.next(‘b’));
–prior方法:取上一个元素的下标,参数是元素下标
dbms_output.put_line(‘取上一个元素的下标prior(’‘b’’):’||c.prior(‘b’));
–打印集合中的元素值
dbms_output.put_line(‘打印下标是b的元素的值c(’‘b’’):’||c(‘b’));
–把下标是b的元素值改为ORACLE
c(‘b’):=‘ORACLE’; --修改元素值
dbms_output.put_line(‘打印下标b的元素值:’||c(‘b’));
–删除集合中的元素
c.delete(); --删除元素
–打印元素个数
dbms_output.put_line(c.count);
end;
–索引表下标会自动排序
declare
–定义一个索引表
type ctype is table of varchar2(30) index by varchar2(20);
–声名一个索引表变量
c ctype;
begin
c(‘a’):=‘smith’;
c(‘1’):=‘king’;
c(‘b’):=‘scott’;
–打印索引表中的第一个元素的下标
dbms_output.put_line(c.first);
end;
索引表的遍历:
declare
–定义了一个存放字符串类型的索引表,它的下标是字符串类型
type ctype is table of varchar2(50) index by varchar2(30);
–声名一个索引表变量
c ctype;
–声名一个循环变量,循环变量的类型和索引表下标类型相同
i varchar2(30);
begin
c(‘a’):=‘SMITH’; --添加一个元素
c(‘b’):=‘KING’;
c(‘c’):=‘SCOTT’;
c(‘d’):=‘tom’;
–给循环变量赋初始值,将第1个元素的下标赋值给循环变量
i:=c.first;
/***
i=a
第1次 打印c(i)=c(a) i=c.last i=d 不成立 i=c.next(i)=b
第2次 打印c(i)=c(b) … i=c.next(i)=c.next(b)=c
第3次 打印c(i)=c© … i=c.next(i)=c.next©=d
第4次 打印c(i)=c(d) i=d 条件成立退出循环
*/
loop
–打印集合中的元素值(循环体语句)
dbms_output.put_line(c(i));
–退出循环语句
exit when i=c.last; --当前循环变量保存的下标和最后一个元素下标相同时退出
–循环控制语句
i:=c.next(i);
end loop;
end;
declare
–定义一个索引表类型
type ctype is table of varchar2(30) index by pls_integer;
–声名一个索引表变量
c ctype;
–声名一个索引表下标类型的循环变量
i pls_integer;
begin
c(1):=‘SMITH’;
c(2):=‘KING’;
c(3):=‘tom’;
c(4):=‘scott’;
–反向遍历
–给循环变量赋值
i:=c.last;
loop
–循环体语句
dbms_output.put_line(c(i));
–退出循环语句
exit when i=c.first;
–循环控制语句
i:=c.prior(i);
end loop;
dbms_output.put_line(rpad(’=’,50,’=’));
—如果索引表的下标是连续的整数时,可以使用for循环遍历
for n in reverse c.first…c.last loop
–循环体语句
dbms_output.put_line(c(n));
end loop;
end;
bulk collect into语句:它可以使用在select into,fetch into,execute immediate into语句中
select 列名,… bulk collect into 集合变量,… from 表名
fetch 游标 bulk collect into 集合变量,…;
execute immediate ‘sql语’ bulk collect into 集合变量;
加上bulk collect之后,以上三个语句可以查询或取出多条数据;注意:索引表用在bulk collect后时,下标只能是整数类型
–写一个代码,根据部门编号,查询部门下的员工姓名,并打印
declare
–定义一个索引表类型
type ctype is table of varchar2(30) index by pls_integer;
–声名一个索引表变量
c ctype;
begin
–查询员工姓名
select ename bulk collect into c from emp where deptno=&部门编号;
–遍历索引表打印姓名
for i in c.first…c.last loop
dbms_output.put_line(c(i));
end loop;
end;
declare
–定义一个索引表类型
type ctype is table of varchar2(30) index by pls_integer;
–声名一个索引表变量
c ctype;
–定义一个游标
cursor cur is select ename from emp where deptno=&部门编号;
begin
–打开游标
open cur;
fetch cur bulk collect into c; --将游标中的所有数据一次性取出,放入集合变量中
–关闭游标
close cur;
–遍历索引表打印姓名
for i in c.first…c.last loop
dbms_output.put_line(c(i));
end loop;
end;
–写一个代码块查询员工的姓名和工作
declare
–定义一个索引表类型
type ctype is table of varchar2(30) index by pls_integer;
–声名两个变量分别保存姓名和工作
v1 ctype; --保存姓名
v2 ctype; --保存工作
begin
select ename,job bulk collect into v1,v2 from emp where deptno=&部门编号;
for i in v1.first…v1.last loop
dbms_output.put_line(v1(i)||’,’||v2(i));
end loop;
end;
–练习题
12,2,56,3,73,34,4,6 写一个代码给这些数进行排序
declare
type ctype is table of number(3) index by pls_integer;
c ctype;
begin
c(1):=12;
c(2):=2;
c(3):=56;
c(4):=3;
c(5):=73;
c(6):=34;
c(7):=4;
c(8):=6;
end;
本文详细介绍了PLSQL中的集合类型,包括索引表的定义、使用方法、遍历方式,以及如何通过bulk collect into语句从查询结果中收集数据到集合中。通过示例展示了如何添加、删除、修改集合元素,以及遍历和打印集合内容。
6593

被折叠的 条评论
为什么被折叠?



