Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

14.1.2 Typical Query Operations

Regardless of the type of cursor, PL/SQL performs the same operations to execute a SQL statement from within your program. In some cases, PL/SQL takes these steps for you. In others, such as with explicit cursors, you will code and execute these steps yourself.

Parse

The first step in processing a SQL statement is to parse it to make sure it is valid and to determine the execution plan (using either the rule-based or cost-based optimizer, depending on how your DBA has set the OPTIMIZER_MODE parameter for your database).

Bind

When you bind, you associate values from your program (host variables) with placeholders inside your SQL statement. With static SQL, the PL/SQL engine itself performs these binds. With dynamic SQL, you must explicitly request a binding of variable values if you wish to use bind variables.

Open

When you open a cursor, the bind variables are used to determine the result set for the SQL statement. The pointer to the active or current row is set to the first row. Sometimes you will not explicitly open a cursor; instead, the PL/SQL engine will perform this operation for you (as with implicit cursors or native dynamic SQL).

Execute

In the execute phase, the statement is run within the SQL engine.

Fetch

If you are performing a query, the FETCH command retrieves the next row from the cursor's result set. Each time you fetch, PL/SQL moves the pointer forward in the result set. When you are working with explicit cursors, remember that FETCH does nothing (does not raise an error) if there are no more rows to retrieve.

Close

This step closes the cursor and releases all memory used by the cursor. Once closed, the cursor no longer has a result set. Sometimes you will not explicitly close a cursor; instead, the PL/SQL engine will perform this operation for you (as with implicit cursors or native dynamic SQL).

Figure 14-1 shows how some of these different operations are used to fetch information from the database into your PL/SQL program.

Figure 14-1. Simplified view of cursor fetch operation

14.1.3 Introduction to Cursor Attributes

This section describes each of the different cursor attributes at a high level. They are explored in more detail for each of the kinds of cursors throughout this chapter, as well as in Chapter 13 and Chapter 15.

PL/SQL offers a total of six cursor attributes, as shown in Table 14-1.

Table 14-1. Cursor attributes

Name

Description

%FOUND

Returns TRUE if record was fetched successfully, FALSE otherwise.

%NOTFOUND

Returns TRUE if record was not fetched successfully, FALSE otherwise.

%ROWCOUNT

Returns number of records fetched from cursor at that point in time.

%ISOPEN

Returns TRUE if cursor is open, FALSE otherwise.

%BULK_ROWCOUNT

Returns the number of records modified by the FORALL statement for each collection element.

%BULK_EXCEPTIONS

Returns exception information for rows modified by the FORALL statement for each collection element.

To reference a cursor attribute, attach it to the name of the cursor or cursor variable about which you want information. Here are some examples:

  • Is the explicit cursor still open?

  • DECLARE

  • CURSOR happiness_cur IS SELECT simple_delights FROM ...;

  • BEGIN

  • OPEN happiness_cur;

  • ...

IF happiness_cur%ISOPEN THEN ...

  • How many rows did I retrieve from the implicit cursor? (Notice that the "name" of my cursor in this case is "SQL".)

  • DECLARE

  • TYPE num_tab IS TABLE OF NUMBER;

  • deptnums num_tab;

  • BEGIN

  • SELECT deptno

  • BULK COLLECT INTO deptnums

  • FROM dept;

  • DBMS_OUTPUT.PUT_LINE (SQL%ROWCOUNT);

END;

The following sections offer brief descriptions of each of the cursor attributes.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]