通过使用参照变量,可以使得应用程序共享相同对象,从而降低占用空间。有两种参照变量:ref cursor 和ref obj_type。
1.ref cursor :当使用显式游标时,需要在定义显式游标时指定相应的select语句,这种显式游标称为静态游标。当使用游标变量时,在定义游标变量时不需要指定select语句,而是在打开游标时指定select语句,从而实现动态的游标操作。如:
declare
type c1 is ref cursor;
emp_cursor c1;
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
open emp_cursor for
selcet ename,sal from emp where depno=10;
loop
fetch emp_cursor into v_ename,v_sal;
exit when emp_cursor%notfound;
dbms_output.put_line(v_ename);
endloop;
close emp_cursor;
end;
如例所示:c1为ref cursor类型,而emp_cursor为游标变量,并且在打开游标变量时指定了其所对应的select语句。
2.ref obj_type:当编写对象类型应用时,为了共享相同对象,可以使用ref引用对象类型,ref实际是指向对象实例的指针。下面通过示例说明如何使用ref。首先建立对象类型home和对象表homes,然后插入数据。如下:
create or replace type home_type is object (
street varchar2(50),city varchar2(20),
state varchar(20),zipcode varchar2(6),
owner varchar2(10));
/
create table home of home_type;
insert into homes values('呼伦北路12号',‘呼和浩特’,‘内蒙’,‘010010’,‘马鸣’);
insert into homes values('呼伦北路13号',‘呼和浩特’,‘内蒙’,‘010010’,‘秦斌’);
commit;
如例所示,对象表homes存放着家庭所在地以及户主姓名。假定每个家庭有四口人,当进行人口统计时,为了使得同一家庭的每个家庭成员可以共享家庭地址,可以使用ref引用home_type对象类型,从而降低占用空间。如:
create table person (
id number(6) primary key,
name varchar2(10),addr ref home_type );
insert into person select 1,'马鸣',ref(p)
from homes p where p.owner='马鸣';
insert into person select 1,'马武',ref(p)
from homes p where p.owner='马鸣';
insert into person select 1,'王敏,ref(p)
from homes p where p.owner='马鸣';
commit;
如例所示,当为person表插入数据是,addr列将会存放指向homes表响应数据的地址指针。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22786751/viewspace-619971/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/22786751/viewspace-619971/