pl/sql [Cursor Management]- Cursor Packages & Managing Cursor Packages

简介: Consolidate Cursors and SQL in Packages By placing cursors inside packages, you control the use of SQL in your PL/SQL code.

Consolidate Cursors and SQL in Packages
By placing cursors inside packages, you control the use of SQL in your PL/SQL code. This removes the burden of knowledge about complex joins, etc., from individual developers. It also allows you to maintain one area in which database structure changes impact PL/SQL programs.
It may not be practical, but it is probably worth attempting to perform an advance analysis of all of the programmers’ needs to produce a full set of queries pre-defined in the package.

1 PACKAGE emp_access
2     IS
3        CURSOR  emp_all ;
4        CURSOR  in_dept_order  ;
5        CURSOR  in_sal_order  ;
6     END  emp_access;
7

Declaring Packaged Cursors
If you are declaring an explicit cursor in a package specification, you have two options:
Declare the entire cursor, including the query, in the specification. This is exactly the same as if you were declaring a cursor in a local PL/SQL block.
Declare only the header of the cursor and do not include the query itself. In this case, the query is defined in the package body only . You have, in effect, hidden the implementation of the cursor.
If you declare only the header, then you must add a RETURN clause to a cursor definition that indicates the data elements returned by a fetch from the cursor. Of course, these data elements are actually determined by the SELECT statement for that cursor, but the SELECT statement appears only in the body, not in the specification.
The RETURN clause may be made up of either of the following datatype structures:

A record defined from a database table using the %ROWTYPE attribute
A record defined from a programmer-defined record
If you declare a cursor in a package body, the syntax is the same as if you were declaring it in a local PL/SQL block.
Here is a simple package specification that shows both of these approaches:

1   CREATE   OR   REPLACE  PACKAGE book_info
2   IS
3    CURSOR  byauthor_cur (
4    author_in  IN  books.author % TYPE
5   )
6    IS
7     SELECT   *
8       FROM  books
9      WHERE  author  =  author_in;
10
11    CURSOR  bytitle_cur (
12    title_filter_in  IN  books.title % TYPE
13   )  RETURN  books % ROWTYPE;
14
15   TYPE author_summary_rt  IS  RECORD (
16    author books.author % TYPE,
17    total_page_count PLS_INTEGER,
18    total_book_count PLS_INTEGER);
19
20    CURSOR  summary_cur (
21    author_in  IN  books.author % TYPE
22   )  RETURN  author_summary_rt;
23   END  book_info;

On lines 3–9 you can see a very typical explicit cursor definition, fully defined in the package specification. On lines 11–13, I define a cursor without a query. In this case, I am telling whoever is looking at the specification that if they open and fetch from this cursor they will receive a single row from the books table for the specified “title filter,” the implication being that wild cards are accepted in the description of the title. On lines 15–18, I define a new record type to hold summary information for a particular author, and on lines 20–22, I declare a cursor that returns summary information (just three values) for a given author.

Working with Packaged Cursors
Now let’s see how you can take advantage of packaged cursors. First of all, you do not need to learn any new syntax to open, fetch from, and close packaged cursors; you just have to remember to prepend the package name to the name of the cursor. So if you want to get information about all the books having to do with PL/SQL, a block can be written like this:

 1 DECLARE
 2    onebook book_info.bytitle_cur % ROWTYPE;
 3 BEGIN
 4     OPEN  book_info.bytitle_cur ( ' %PL/SQL% ' );
 5    LOOP
 6        EXIT   WHEN  book_info.bytitle_cur % NOTFOUND;
 7        FETCH  book_info.bytitle_cur  INTO  onebook;
 8       book_info.display (onebook);
 9     END  LOOP;
10     CLOSE  book_info.bytitle_cur;
11 END ;

As you can see, you can %ROWTYPE a packaged cursor and check its attributes just as you would with a locally defined explicit cursor.

There are some hidden issues lurking in this code, however. Because the cursor is declared in a package specification, its scope is not bound to any given PL/SQL block. Suppose that this code is run:

1 BEGIN   --  Only open
2     OPEN  book_info.bytitle_cur ( ' %PEACE% ' );
3 END ;

and then, in the same session, the anonymous block with the LOOP shown above is also run. An error will then occur:

ORA-06511: PL/SQL: cursor already open
This happened because in the “only open” block, the cursor was not closed. Even though the block terminated, the packaged cursor did not close.

Given the persistence of packaged cursors, you should always keep the following rules in mind:

Never assume that a packaged cursor is closed (and ready to be opened).
Never assume that a packaged cursor is opened (and ready to be closed).
Always be sure to explicitly close your packaged cursor when you are done with it.
These three rules also apply to working with other kinds of cursors—such as locally defined explicit cursors and DBMS_SQL cursors—but they are absolutely crucial for packaged cursors.
If you neglect these rules, you might well execute an application that makes certain assumptions, and then pays the price in unexpected and unhandled exceptions.

目录
相关文章
|
7月前
|
SQL 存储 Oracle
Oracle的PL/SQL定义变量和常量:数据的稳定与灵动
【4月更文挑战第19天】在Oracle PL/SQL中,变量和常量扮演着数据存储的关键角色。变量是可变的“魔术盒”,用于存储程序运行时的动态数据,通过`DECLARE`定义,可在循环和条件判断中体现其灵活性。常量则是不可变的“固定牌”,一旦设定值便保持不变,用`CONSTANT`声明,提供程序稳定性和易维护性。通过 `%TYPE`、`NOT NULL`等特性,可以更高效地管理和控制变量与常量,提升代码质量。善用两者,能优化PL/SQL程序的结构和性能。
|
7月前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL隐式游标:数据的“自动导游”与“轻松之旅”
【4月更文挑战第19天】Oracle PL/SQL中的隐式游标是自动管理的数据导航工具,简化编程工作,尤其适用于简单查询和DML操作。它自动处理数据访问,提供高效、简洁的代码,但不适用于复杂场景。显式游标在需要精细控制时更有优势。了解并适时使用隐式游标,能提升数据处理效率,让开发更加轻松。
|
1月前
|
SQL 数据采集 监控
局域网监控电脑屏幕软件:PL/SQL 实现的数据库关联监控
在当今网络环境中,基于PL/SQL的局域网监控系统对于企业和机构的信息安全至关重要。该系统包括屏幕数据采集、数据处理与分析、数据库关联与存储三个核心模块,能够提供全面而准确的监控信息,帮助管理者有效监督局域网内的电脑使用情况。
22 2
|
5月前
|
SQL 存储 监控
|
4月前
|
SQL Oracle 关系型数据库
SQL与PL/SQL:数据库编程语言的比较
【8月更文挑战第31天】
81 0
|
5月前
|
SQL 供应链 安全
Hospital Management Startup 1.0 SQL 注入(CVE-2022-23366)
Hospital Management Startup 1.0 SQL 注入(CVE-2022-23366)
|
5月前
|
SQL 安全 网络安全
Hospital Management System v4.0 SQL 注入(CVE-2022-24263)
Hospital Management System v4.0 SQL 注入(CVE-2022-24263)
|
6月前
|
SQL 数据库 Perl
PL/SQL中执行按钮变为灰色后如何恢复【已解决】
PL/SQL中执行按钮变为灰色后如何恢复【已解决】
529 0
|
7月前
|
SQL Oracle 关系型数据库
Oracle的PL/SQL游标属性:数据的“导航仪”与“仪表盘”
【4月更文挑战第19天】Oracle PL/SQL游标属性如同车辆的导航仪和仪表盘,提供丰富信息和控制。 `%FOUND`和`%NOTFOUND`指示数据读取状态,`%ROWCOUNT`记录处理行数,`%ISOPEN`显示游标状态。还有`%BULK_ROWCOUNT`和`%BULK_EXCEPTIONS`增强处理灵活性。通过实例展示了如何在数据处理中利用这些属性监控和控制流程,提高效率和准确性。掌握游标属性是提升数据处理能力的关键。
|
7月前
|
SQL Oracle 安全
Oracle的PL/SQL循环语句:数据的“旋转木马”与“无限之旅”
【4月更文挑战第19天】Oracle PL/SQL中的循环语句(LOOP、EXIT WHEN、FOR、WHILE)是处理数据的关键工具,用于批量操作、报表生成和复杂业务逻辑。LOOP提供无限循环,可通过EXIT WHEN设定退出条件;FOR循环适用于固定次数迭代,WHILE循环基于条件判断执行。有效使用循环能提高效率,但需注意避免无限循环和优化大数据处理性能。掌握循环语句,将使数据处理更加高效和便捷。