Implicit Cursor Life Cycle FAQS

1. What is an implicit cursor in Oracle?

An implicit cursor is automatically created and managed by Oracle whenever a DML statement (INSERT, UPDATE, DELETE) or a single-row SELECT INTO statement is executed.

  • No need to explicitly declare, open, fetch, or close.
  • Oracle internally handles the cursor’s life cycle.

2. What are the steps in the life cycle of an implicit cursor?

The implicit cursor life cycle has three main stages:

Stage

Description

Creation & Parsing

Oracle creates the cursor in memory and parses the SQL statement.

Execution & Fetching

SQL statement is executed. For SELECT INTO, data is fetched into variables. For DML, rows are affected.

Closing

Cursor is automatically closed once statement execution completes.

Implicit cursors are automatically closed by Oracle; you don’t need to close them manually.

3. Life cycle example with a DML statement

BEGIN

   UPDATE employees

   SET salary = salary * 1.10

   WHERE department_id = 50;

   DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows updated.');

END;

Life Cycle Steps:

1.    Creation & Parsing – Oracle creates the implicit cursor for the UPDATE.

2.    Execution & Fetching – Statement runs, rows updated, SQL%ROWCOUNT is set.

3.    Closing – Oracle automatically closes the cursor after execution.

4. Can an implicit cursor be reused?

  • No, each implicit cursor is tied to a single SQL statement.
  • Each new SQL statement creates a new implicit cursor.

UPDATE employees SET salary = salary * 1.1 WHERE department_id = 10;

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);

UPDATE employees SET salary = salary * 1.05 WHERE department_id = 20;

DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT);

  • Two implicit cursors are created here, one for each UPDATE.

5. Attributes of an implicit cursor during its life cycle

Attribute

Description

When Valid

SQL%FOUND

TRUE if at least one row affected

After execution

SQL%NOTFOUND

TRUE if no rows affected

After execution

SQL%ROWCOUNT

Number of rows affected/fetched

After execution

SQL%ISOPEN

Always FALSE

Implicit cursors auto-close

%ISOPEN is always FALSE because implicit cursors do not stay open after execution.

6. Life cycle differences: SELECT INTO vs DML

Type

Execution Stage

Fetching

Closing

SELECT INTO

SQL parsed and executed

Data fetched into variables

Cursor auto-closes

DML (INSERT/UPDATE/DELETE)

SQL parsed and executed

Rows affected

Cursor auto-closes

7. What happens if a SELECT INTO returns no rows?

  • Oracle raises NO_DATA_FOUND exception.
  • The implicit cursor still completes its life cycle and closes automatically.

8. What happens if a SELECT INTO returns multiple rows?

  • Oracle raises TOO_MANY_ROWS exception.
  • Cursor still closes automatically after the exception.

9. Can we check the life cycle programmatically?

Yes, using SQL cursor attributes immediately after execution:

BEGIN

   DELETE FROM employees WHERE department_id = 999;

 

   IF SQL%NOTFOUND THEN

      DBMS_OUTPUT.PUT_LINE('No rows deleted.');

   ELSE

      DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows deleted.');

   END IF;

   DBMS_OUTPUT.PUT_LINE('Is cursor open? ' || SQL%ISOPEN);

END;

Output:

No rows deleted.

Is cursor open? FALSE

10. Common mistakes related to implicit cursor life cycle

  • Assuming %ISOPEN can be TRUE
  • Using %FOUND before statement execution
  • Expecting implicit cursor to fetch multiple rows (SELECT INTO fetches only one row)
  • Forgetting to handle exceptions (NO_DATA_FOUND, TOO_MANY_ROWS)

11. Best practices

  • Always handle exceptions for SELECT INTO
  • Use %ROWCOUNT to validate DML operations
  • Use implicit cursors for single-row queries or DML operations
  • Use explicit cursors for multi-row processing

 

No comments:

Post a Comment