当前位置:   article > 正文

数据集合 oracle,oracle集合

oracle 集合获取下一个下标

oracle集合

1初识集合

集合是oracle中的一种数据类型 存放一组数据类型相同的数据

集合组成

由下标和值组成

下标的类型包含数字(整数,pls_integer,binary_integer)和字符串

值的类型可以是数据库中的所有类型(基本数据类型,记录类型(record,%rowtype),%type,集合类型)

集合三种类型

索引表可以通过数字或字符串作为下标来查找其中的元素,仅在pl/sql中使用

嵌套表拥有索引表的所有特性,但是下标只能是数字类型(连续的整数)

可以在plsql中使用也可以在数据库中使用

变长数组下标只能是数字类型,创建时需要指定最大长度

可以在plsql中使用也可以在数据库中使用

2索引表类型

2.1语法

定义一个索引表类型

type 类型名称 is table of 元素值的数据类型 index by 下标的数据类型;

索引变量的声明

变量名 类型名;

索引变量的使用

变量名(下标);

2.2简单使用declare

--定义一个索引表类型

type itype is table of varchar2(30) index by pls_integer;

--声明一个集合变量

ind itype;

begin

ind(1):='张三';

ind(4):='李四';

dbms_output.put_line(ind(4)||','||ind(1));

end;

输出李四,张三

3集合的属性或方法

3.1属性或方法简单汇总first取集合第一个元素的下标

last取集合元素最后一个元素的下标

next(下标)取集合当前下标的下一个元素的下标

prior(下标)取集合当前下标的上一个元素的下标

count取集合中元素的个数

delete删除集合中的元素

limit取集合最大的元素的个数(变长数组)

3.2属性或方法示例declare

--定义一个集合类型

type itype is table of varchar2(30) index by varchar2(30);

--声明一个索引表

eng itype;

begin

--给索引表赋值

eng('a'):='张三';

eng('b'):='李四';

eng('c'):='王五';

eng('d'):='赵六';

--打印集合中第一个元素的下标

dbms_output.put_line('第一个元素的下标: '||eng.first);

--打印集合中最后一个一个元素的下标

dbms_output.put_line('最后一个元素的下标: '||eng.last);

--打印集合中第二个元素的下标

dbms_output.put_line('第二个元素的下标: '||eng.next(eng.first));

--打印集合中倒数第二个元素的下标

dbms_output.put_line('倒数第二个元素的下标: '||eng.prior(eng.last));

--打印集合中元素的个数

dbms_output.put_line('元素个数: '||eng.count);

end;

输出第一个元素的下标: a

最后一个元素的下标: d

第二个元素的下标: b

倒数第二个元素的下标: c

元素个数: 4declare

--定义一个集合类型

type itype is table of varchar2(30) index by varchar2(10);

--定义一个变量保存集合的下标

v_ind varchar(10);

--声明一个索引表

eng itype;

begin

--给索引赋值

eng('a'):='张三';

eng('b'):='李四';

eng('c'):='王五';

eng('d'):='赵六';

--遍历打印集合中的元素

--将第一个元素的下标放入变量v_ind中

v_ind:=eng.first;

loop

--打印集合元素

dbms_output.put_line(eng(v_ind));

--判断退出条件,当下标的值等于最后一个下标的值

exit when v_ind=eng.last;

--循环控制语句

v_ind:=eng.next(v_ind);

end loop;

end;

输出:张三

李四

王五

赵六

4bulk collect语句循环遍历

集合提供了bulk collect语句获取表中数据

通过bulk colleck语句存入集合中的元素下标从1开始并且是连续的

4.1语法1

select 列.. bulk collect into 集合变量 from 表 where条件

获取emp表中30部门中的员工号和姓名declare

--定义索引表集合存储emp表中empno和ename

type i_empno is table of emp.empno%type index by pls_integer;

type i_ename is table of emp.ename%type index by pls_integer;

--定义索引变量

eno i_empno;

eme i_ename;

begin

--bull collect语句获取empno和ename

select empno,ename bulk collect into eno,eme from emp where deptno=30;

for i in eno.first..eno.last loop

dbms_output.put_line(eno(i)||eme(i));

end loop;

end;

输出7499ALLEN

7521WARD

7654MARTIN

7698BLAKE

7844TURNER

7900JAMES

4.2语法2

execute immediate 'select语句' bulk collect into 集合变量

获取emp表中30部门中的员工号和姓名declare

--声明一个变量存放selqct查询结果

v_sql varchar2(255);

--定义索引表集合存储emp表中empno和ename

type i_empno is table of emp.empno%type index by pls_integer;

type i_ename is table of emp.ename%type index by pls_integer;

--定义索引变量

eno i_empno;

eme i_ename;

begin

--将sql语句赋值给v_sql

v_sql:='select empno,ename from emp where deptno=30';

--bulk collect语句,将v_sql查询到的结果放到eno和eme中

execute immediate v_sql bulk collect into eno,eme;

--循环打印eno和eme中所有的数据

for i in eno.first..eme.last loop

dbms_output.put_line(eno(i)||eme(i));

end loop;

end;

输出7499ALLEN

7521WARD

7654MARTIN

7698BLAKE

7844TURNER

7900JAMES

4.3语法3

fetch 游标 bulk collect into 集合变量

获取emp表中30部门中的员工号和姓名declare

--声明一个游标

cursor cur is select empno,ename from emp where deptno=30;

--声明集合,存放empno和ename

type i_empno is table of emp.empno%type index by pls_integer;

type i_ename is table of emp.ename%type index by pls_integer;

--声明索引变量

eno i_empno;

eme i_ename;

begin

--打开游标

open cur;

--bulk collect语句,将cur查询到的结果放到eno和eme中

fetch cur bulk collect into eno,eme;

--关闭游标

close cur;

--循环打印出eno和eme集合中的所有数据

for i in eno.first..eno.last loop

dbms_output.put_line(eno(i)||','||eme(i));

end loop;

end;

输出7499,ALLEN

7521,WARD

7654,MARTIN

7698,BLAKE

7844,TURNER

7900,JAMES

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/780555
推荐阅读
相关标签
  

闽ICP备14008679号