oracle cusor 定义

定义

 1.type cursorType is ref cursor;

2.procedure getApolloPortolioAllPortfolios(
    p_portfolios out sys_refcursor
  );

http://orajc.blog.51cto.com/458434/105931

 

游标是数据库中一个命名的工作区,当游标被声明后,他就与一个固定的SQL想关联,在编译时刻是已知的,是静态的.它永远指向一个相同的查询工作区.
游标变量可以在运行时刻与不同的SQL语句关联,在运行时可以取不同的SQL语句.它可以引用不同的工作区.


游标和游标变量是不能相互代替的.
如何定义游标类型
TYPE ref_type_name IS REF CURSOR [RETURN return_type];
声明游标变量
cursor_name ref_type_name;
 
ref_type_name 是后面声明游标变量时要用到的我们的游标类型(自定义游标类型,即CURSOR是系统默认 的, ref_type_name是我们定义的 );return_type代表数据库表中的一行,或一个记录类型
TYPE ref_type_name IS REF CURSOR RETURN employee%TYPE
RETURN 是可选的,如果有是强类型,可以减少错误,如果没有return是弱引用,有较好的灵活性.
 
不能在包头里面声明游标变量,注意,但可以定义游标类型,要注意这二者的区别.
可以声明游标变量作为函数或过程的形式参数.
%TYPE一个列类型
%ROWTYPE行类型
控制游标变量
OPEN-FOR(打开游标变量,与多行查询连接起来) FETCH(从结果集中取行数据),close(关闭游标变量)
BULK COLLECT子句将游标变量中的行一次性提取到一个集合中.
 
游标遍历:
1.

declare

cursor emp_cur ( p_deptid in number) is

select * from employees where department_id = p_deptid;

open emp_cur(30);

loop

  fetch emp_cur into l_emp;

  exit when emp_cur%notfound;

  dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');

  dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);

end loop;

close emp_cur;

 

2.for rec_ptrs in (select * from pvtrs order by pvtrsid asc)
 loop
  dbms_output.put_line('pvtrsid:'||rec_ptrs.pvtrsid);
  --feedOasys
  begin
   select count(*) into vcount from REPORTS_PORTFOLIOS_MAPPING where REPORTID=1 and PVTRSID=rec_ptrs.pvtrsid;
   if (vcount > 0) then
    vfeedOasys := 'Y';
   else
    vfeedOasys := 'N';
   end if;
   if (vfeedOasys = 'Y') then
    --feed config
    vMTM := 'N';
    vIA := 'N';
    vnotionalComitment := 'N';
    begin
     select MTMFEED,IAFEED,NCFEED into feedRec from OASYS_FEEDS_PORTFOLIOS_CONFIG where PVTRSID=rec_ptrs.pvtrsid;
     if (feedRec.MTMFEED = 'Y') then
      vMTM := 'Y';
     end if;
     if (feedRec.IAFEED = 'Y') then
      vIA := 'Y';
     end if;
     if (feedRec.NCFEED = 'Y') then
      vnotionalComitment := 'Y';
     end if;
    exception
     when NO_DATA_FOUND then
         vMTM := 'N';
      vIA := 'N';
      vnotionalComitment := 'N';
    end;
    dbms_output.put_line('vMTM,vIA,vnotionalComitment:'||vMTM||','||vIA||','||vnotionalComitment);
   else
    vMTM := 'N';
    vIA := 'N';
    vnotionalComitment := 'N';
   end if;
  exception
      when NO_DATA_FOUND then
       vfeedOasys := 'N';
  end;
  dbms_output.put_line('vfeedOasys:'||vfeedOasys);  
 end loop;

 

3. open p_data for
     select ASOFDATE, ISSUERNAME, NOTIONALAMOUNTPCCY, WAGCURRENTPRICE,
            WAGCURRENTPRICE - nvl(findIssuerDatePrice(dayBefore, ISSUERNAME),nvl(findIssuerDatePrice(dayBeforeminusone, ISSUERNAME), nvl(findIssuerDatePrice(dayBeforeminustwo, ISSUERNAME), findIssuerDatePrice(dayBeforeminusthree, ISSUERNAME))))    DailyChange,
            WAGCURRENTPRICE - nvl(findIssuerDatePrice(weekBefore, ISSUERNAME),nvl(findIssuerDatePrice(weekBeforeminusone, ISSUERNAME), nvl(findIssuerDatePrice(weekBeforeminustwo, ISSUERNAME),findIssuerDatePrice(weekBeforeminusthree, ISSUERNAME)))) WeeklyChange,
            WAGCURRENTPRICE - nvl(findIssuerDatePrice(monthBefore, ISSUERNAME),nvl(findIssuerDatePrice(monthBeforeminusone, ISSUERNAME), nvl(findIssuerDatePrice(monthBeforeminustwo, ISSUERNAME), findIssuerDatePrice(monthBeforeminusthree, ISSUERNAME)))) MonthlyChange
     from (
          select ASOFDATE, ISSUERNAME, NOTIONALAMOUNTPCCY, WAGCURRENTPRICE
          from Rpt_IssuerNotionalPriceHIST
          where ASOFDATE = closeDate
          order by NOTIONALAMOUNTPCCY desc, ISSUERNAME
          )
    where ROWNUM <= 10;

Oracle Cursors语法总结

关于Oracle Cursors的语法总结,Oracle Cursors是用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作。

一.Oracle的Cursors概念:

游标:用来查询数据库,获取记录集合(结果集)的指针,可以让开发者一次访问一行结果集,在每条结果集上作操作。

 

二.Oracle的Cursors分类:

1.静态游标:

分为显式游标和隐式游标。

2.REF游标:

是一种引用类型,类似于指针。

 

三.Oracle的Cursors详细内容:

1.显式游标:

CURSOR游标名(参数) [返回值类型] IS

Select语句

生命周期:

a.打开游标(OPEN)

解析,绑定。。。不会从数据库检索数据

b.从游标中获取记录(FETCH INTO)

执行查询,返回结果集。通常定义局域变量作为从游标获取数据的缓冲区。

c.关闭游标(CLOSE)

完成游标处理,用户不能从游标中获取行。还可以重新打开。

选项:参数和返回类型

set serveroutput on

declare

cursor emp_cur ( p_deptid in number) is

select * from employees where department_id = p_deptid;

l_emp employees%rowtype;

begin

dbms_output.put_line('Getting employees from department 30');

open emp_cur(30);

loop

  fetch emp_cur into l_emp;

  exit when emp_cur%notfound;

  dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');

  dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);

end loop;

close emp_cur;

dbms_output.put_line('Getting employees from department 90');

open emp_cur(90);

loop

  fetch emp_cur into l_emp;

  exit when emp_cur%notfound;

  dbms_output.put_line('Employee id '|| l_emp.employee_id || ' is ');

  dbms_output.put_line(l_emp.first_name || ' ' || l_emp.last_name);

end loop;

close emp_cur;

end;

/

 

2.隐式游标:

不用明确建立游标变量,分两种:

a.在PL/SQL中使用DML语言,使用ORACLE提供的名为SQL的隐示游标

b.CURSOR FOR LOOP,用于for loop语句

a.举例:

declare

begin

update departments set department_name=department_name;

--where 1=2;

 

dbms_output.put_line('update '|| sql%rowcount ||' records');

end;

/

 

b.举例:

declare

begin

for my_dept_rec in ( select department_name, department_id from departments)

loop

  dbms_output.put_line(my_dept_rec.department_id || ' : ' || my_dept_rec.department_name);

end loop;

end;

/

 

c.举例:

单独select

declare

l_empno emp.EMPLOYEE_ID%type;

-- l_ename emp.ename%type;

begin

select EMPLOYEE_ID  

  into l_empno

from emp;

--where rownum =1;

dbms_output.put_line(l_empno);

end;

/

 

使用INTO获取值,只能返回一行。

 

游标属性:

%FOUND:变量最后从游标中获取记录的时候,在结果集中找到了记录。

%NOTFOUND:变量最后从游标中获取记录的时候,在结果集中没有找到记录。

%ROWCOUNT:当前时刻已经从游标中获取的记录数量。

%ISOPEN:是否打开。

Declare

Cursor emps is

Select * from employees where rownum<6 order by 1;

 

Emp employees%rowtype;

Row number :=1;

Begin

Open emps;

Fetch emps into emp;

 

Loop

  If emps%found then

   Dbms_output.put_line('Looping over record '||row|| ' of ' || emps%rowcount);

   Fetch emps into emp;

   Row := row + 1;

  Elsif emps%notfound then

   Exit;  ---exit loop, not IF

  End if;

End loop;

 

If emps%isopen then

  Close emps;

End if;

End;

/

 

显式和隐式游标的区别:

尽量使用隐式游标,避免编写附加的游标控制代码(声明,打开,获取,关闭),也不需要声明变量来保存从游标中获取的数据。

 

3.REF CURSOR游标:

动态游标,在运行的时候才能确定游标使用的查询。分类:

1.强类型(限制)REF CURSOR,规定返回类型

2.弱类型(非限制)REF CURSOR,不规定返回类型,可以获取任何结果集。

TYPE ref_cursor_name IS REF CURSOR [RETURN return_type]

 

Declare

Type refcur_t is ref cursor;

 

Type emp_refcur_t is ref cursor return employee%rowtype;

Begin

Null;

End;

/

 

强类型举例:

declare

--声明记录类型

type emp_job_rec is record(

  employee_id number,

  employee_name varchar2(50),

  job_title varchar2(30)

);

--声明REF CURSOR,返回值为该记录类型

type emp_job_refcur_type is ref cursor

  return emp_job_rec;

--定义REF CURSOR游标的变量

emp_refcur emp_job_refcur_type;

emp_job emp_job_rec;

begin

open emp_refcur for

  select e.employee_id,

    e.first_name || ' ' ||e.last_name "employee_name",

    j.job_title

  from employees e, jobs j

  where e.job_id = j.job_id and rownum < 11 order by 1;

fetch emp_refcur into emp_job;

while emp_refcur%found loop

  dbms_output.put_line(emp_job.employee_name || '''s job is ');

  dbms_output.put_line(emp_job.job_title);

  fetch emp_refcur into emp_job;

end loop;

end;

/

 

本篇文章来源于 PHP资讯 原文链接:http://www.phpq.net/oracle/oracle-cursors-syntax.html

=================

属性

http://www.cnblogs.com/zndavid/archive/2009/06/09/1499601.html

当执行一条DML语句后,DML语句的结果保存在四个游标属性中,这些属性用于控制程序流程或者了解程序的状态。当运行DML语句时,PL/SQL打开一个内建游标并处理结果,游标是维护查询结果的内存中的一个区域,游标在运行DML语句时打开,完成后关闭。隐式游标只使用SQL%FOUND,SQL%NOTFOUND,SQL%ROWCOUNT三个属性.SQL%FOUND,SQL%NOTFOUND是布尔值,SQL%ROWCOUNT是整数值。

  SQL%FOUND和SQL%NOTFOUND

  在执行任何DML语句前SQL%FOUND和SQL%NOTFOUND的值都是NULL,在执行DML语句后,SQL%FOUND的属性值将是:

  . TRUE :INSERT

  . TRUE :DELETE和UPDATE,至少有一行被DELETE或UPDATE.

  . TRUE :SELECT INTO至少返回一行

  当SQL%FOUND为TRUE时,SQL%NOTFOUND为FALSE。

  SQL%ROWCOUNT

  在执行任何DML语句之前,SQL%ROWCOUNT的值都是NULL,对于SELECT INTO语句,如果执行成功,SQL%ROWCOUNT的值为1,如果没有成功,SQL%ROWCOUNT的值为0,同时产生一个异常NO_DATA_FOUND.

  SQL%ISOPEN

  SQL%ISOPEN是一个布尔值,如果游标打开,则为TRUE, 如果游标关闭,则为FALSE.对于隐式游标而言SQL%ISOPEN总是FALSE,这是因为隐式游标在DML语句执行时打开,结束时就立即关闭。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值