复杂的数据类型

版权声明:欢迎指正,评论,共同学习 https://blog.csdn.net/m18994118189/article/details/82906569

1.行记录类型

%rowtype

代表一行的记录结构

%前是表的名称

便于存储表中的一行

对于这种变量的调用,就像我们正常去获取表中一行里的值一样

 

declare

v1 dept%rowtype;

begin

select * into v1 from dept where deptno=10;

dbms_output.put_line(v1.deptno);

dbms_output.put_line(v1.dname);

dbms_output.put_line(v1.loc);

dbms_output.put_line(v1.deptno||' '||v1.dname||' '||v1.loc);

end;

/

 

2.PLSQL表  集合 数组

确切的讲,PLSQL表更像一个有主键约束(索引)的表,通过主键来访问数据

包含两个要素:

主键(元素的编号),数据类型为binary_integer

成员或者叫元素,可以为简单的变量,也可以是复杂的变量

 

简单变量的例子

declare

type t1 is table of emp.ename%type index by binary_integer;

type t2 is table of date index by binary_integer;

v1 t1;

v2 t2;

begin

v1(1):='SCOTT';

v2(1):=sysdate;

select ename,hiredate into v1(2),v2(2) from emp where empno=7900;

dbms_output.put_line(v1(1)||'                '||v1(2));

dbms_output.put_line(v2(1)||'                '||v2(2));

end;

/

 

复杂变量的例子

declare

type t1 is table of dept%rowtype index by binary_integer;

v1 t1;

begin

select * into v1(10) from dept where deptno=10;

select * into v1(20) from dept where deptno=20;

select * into v1(30) from dept where deptno=30;

select * into v1(40) from dept where deptno=40;

dbms_output.put_line(v1(10).deptno||' '||v1(10).dname||' '||v1(10).loc);

dbms_output.put_line(v1(20).deptno||' '||v1(20).dname||' '||v1(20).loc);

dbms_output.put_line(v1(30).deptno||' '||v1(30).dname||' '||v1(30).loc);

dbms_output.put_line(v1(40).deptno||' '||v1(40).dname||' '||v1(40).loc);

end;

/

 

PLSQL TABLE的属性

PRIOR:指定成员之前那个成员的编号

NEXT:指定成员之后那个成员的编号

COUNT:统计PLSQL TABLE中有多少个成员

FIRST:第一个成员编号是多少

LAST:最后一个编号是多少

DELETE:从PLSQL TABLE里删除成员,不写成员编号的话,就是删除全部成员

EXISTS:判断成员编号是否PLSQL TABLE中

 

 

declare

type t1 is table of dept%rowtype index by binary_integer;

v1 t1;

n1 number(3);

n2 number(3);

n3 number(3);

n4 number(3);

n5 number(3);

n6 number(3);

n7 number(3);

begin

select * into v1(10) from dept where deptno=10;

select * into v1(20) from dept where deptno=20;

select * into v1(30) from dept where deptno=30;

select * into v1(40) from dept where deptno=40;

n1:=v1.first;

n2:=v1.last;

n3:=v1.count;

n4:=v1.prior(20);

n5:=v1.next(20);

dbms_output.put_line(n1||' '||n2||' '||n3||' '||n4||' '||n5);

v1.delete(40);

n6:=v1.count;

dbms_output.put_line(n6);

end;

/

 

declare

type t1 is table of emp.ename%type index by binary_integer;

type t2 is table of date index by binary_integer;

v1 t1;

v2 t2;

begin

v1(1):='SCOTT';

v2(1):=sysdate;

select ename,hiredate into v1(7),v2(7) from emp where empno=7900;

select ename,hiredate into v1(12),v2(12) from emp where empno=7788;

for i in 1..10 loop

if v1.exists(i) then

dbms_output.put_line(v1(i));

end if;

if v2.exists(i) then

dbms_output.put_line(v2(i));

end if;

end loop;

end;

/

猜你喜欢

转载自blog.csdn.net/m18994118189/article/details/82906569
今日推荐