数据集合 oracle,oracle集合
oracle集合
1初識集合
集合是oracle中的一種數(shù)據(jù)類型 存放一組數(shù)據(jù)類型相同的數(shù)據(jù)
集合組成
由下標(biāo)和值組成
下標(biāo)的類型包含數(shù)字(整數(shù),pls_integer,binary_integer)和字符串
值的類型可以是數(shù)據(jù)庫中的所有類型(基本數(shù)據(jù)類型,記錄類型(record,%rowtype),%type,集合類型)
集合三種類型
索引表可以通過數(shù)字或字符串作為下標(biāo)來查找其中的元素,僅在pl/sql中使用
嵌套表擁有索引表的所有特性,但是下標(biāo)只能是數(shù)字類型(連續(xù)的整數(shù))
可以在plsql中使用也可以在數(shù)據(jù)庫中使用
變長數(shù)組下標(biāo)只能是數(shù)字類型,創(chuàng)建時需要指定最大長度
可以在plsql中使用也可以在數(shù)據(jù)庫中使用
2索引表類型
2.1語法
定義一個索引表類型
type 類型名稱 is table of 元素值的數(shù)據(jù)類型 index by 下標(biāo)的數(shù)據(jù)類型;
索引變量的聲明
變量名 類型名;
索引變量的使用
變量名(下標(biāo));
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取集合第一個元素的下標(biāo)
last取集合元素最后一個元素的下標(biāo)
next(下標(biāo))取集合當(dāng)前下標(biāo)的下一個元素的下標(biāo)
prior(下標(biāo))取集合當(dāng)前下標(biāo)的上一個元素的下標(biāo)
count取集合中元素的個數(shù)
delete刪除集合中的元素
limit取集合最大的元素的個數(shù)(變長數(shù)組)
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'):='趙六';
--打印集合中第一個元素的下標(biāo)
dbms_output.put_line('第一個元素的下標(biāo): '||eng.first);
--打印集合中最后一個一個元素的下標(biāo)
dbms_output.put_line('最后一個元素的下標(biāo): '||eng.last);
--打印集合中第二個元素的下標(biāo)
dbms_output.put_line('第二個元素的下標(biāo): '||eng.next(eng.first));
--打印集合中倒數(shù)第二個元素的下標(biāo)
dbms_output.put_line('倒數(shù)第二個元素的下標(biāo): '||eng.prior(eng.last));
--打印集合中元素的個數(shù)
dbms_output.put_line('元素個數(shù): '||eng.count);
end;
輸出第一個元素的下標(biāo): a
最后一個元素的下標(biāo): d
第二個元素的下標(biāo): b
倒數(shù)第二個元素的下標(biāo): c
元素個數(shù): 4declare
--定義一個集合類型
type itype is table of varchar2(30) index by varchar2(10);
--定義一個變量保存集合的下標(biāo)
v_ind varchar(10);
--聲明一個索引表
eng itype;
begin
--給索引賦值
eng('a'):='張三';
eng('b'):='李四';
eng('c'):='王五';
eng('d'):='趙六';
--遍歷打印集合中的元素
--將第一個元素的下標(biāo)放入變量v_ind中
v_ind:=eng.first;
loop
--打印集合元素
dbms_output.put_line(eng(v_ind));
--判斷退出條件,當(dāng)下標(biāo)的值等于最后一個下標(biāo)的值
exit when v_ind=eng.last;
--循環(huán)控制語句
v_ind:=eng.next(v_ind);
end loop;
end;
輸出:張三
李四
王五
趙六
4bulk collect語句循環(huán)遍歷
集合提供了bulk collect語句獲取表中數(shù)據(jù)
通過bulk colleck語句存入集合中的元素下標(biāo)從1開始并且是連續(xù)的
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查詢結(jié)果
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查詢到的結(jié)果放到eno和eme中
execute immediate v_sql bulk collect into eno,eme;
--循環(huán)打印eno和eme中所有的數(shù)據(jù)
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 游標(biāo) bulk collect into 集合變量
獲取emp表中30部門中的員工號和姓名declare
--聲明一個游標(biāo)
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
--打開游標(biāo)
open cur;
--bulk collect語句,將cur查詢到的結(jié)果放到eno和eme中
fetch cur bulk collect into eno,eme;
--關(guān)閉游標(biāo)
close cur;
--循環(huán)打印出eno和eme集合中的所有數(shù)據(jù)
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
總結(jié)
以上是生活随笔為你收集整理的数据集合 oracle,oracle集合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle dbms lob,如何使用
- 下一篇: oracle判断侦听状态,oracle