1. 程式人生 > >複雜的資料型別

複雜的資料型別

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;

/