Chinaunix首页 | 论坛 | 博客
  • 博客访问: 338895
  • 博文数量: 94
  • 博客积分: 1500
  • 博客等级: 上尉
  • 技术积分: 1020
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-11 09:23
文章分类
文章存档

2011年(76)

2010年(18)

分类: Oracle

2011-02-12 19:21:02

 
Declaring REF CURSOR Types and Cursor Variables
To create cursor variables, you define a REF CURSOR type, then declare cursor variables of that type. You can define REF CURSOR types in any PL/SQL block, subprogram, or package. In the following example, you declare a REF CURSOR type that represents a result set from the DEPARTMENTS table:

DECLARE
  TYPE DeptCurTyp IS REF CURSOR RETURN departments%ROWTYPE;
REF CURSOR types can be strong (with a return type) or weak (with no return type). Strong REF CURSOR types are less error prone because the PL/SQL compiler lets you associate a strongly typed cursor variable only with queries that return the right set of columns. Weak REF CURSOR types are more flexible because the compiler lets you associate a weakly typed cursor variable with any query. Because there is no type checking with a weak REF CURSOR, all such types are interchangeable. Instead of creating a new type, you can use the predefined type SYS_REFCURSOR.
Once you define a REF CURSOR type, you can declare cursor variables of that type in any PL/SQL block or subprogram.
 
----------------------------------------------------------------
 
An example:
 
create or replace procedure cal(tb varchar2) is
id pls_integer;
total pls_integer := 0;
type emp_cur is ref cursor;
cur emp_cur;
begin
open cur for 'select employee_id from ' || tb;
loop
fetch cur into id;
exit when cur%notfound;
total := total + id;
end loop;
close cur;
dbms_output.put_line(total) ;
end ;
阅读(3118) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-03-05 13:39:12

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com