Updated on 2023-10-31 GMT+08:00

Explicit Cursor

An explicit cursor is used to process query statements, particularly when the query results contain multiple records.

Procedure

An explicit cursor performs the following six PL/SQL steps to process query statements:

  1. Define a static cursor: Define a cursor name and its corresponding SELECT statement.

    Figure 1 shows the syntax diagram for defining a static cursor.

    Figure 1 static_cursor_define::=

    Parameter description:

    • cursor_name: defines a cursor name.
    • parameter: specifies cursor parameters. Only input parameters are allowed in the following format:
      parameter_name datatype
    • select_statement: specifies a query statement.

    The system automatically determines whether the cursor can be used for backward fetches based on the execution plan.

    Define a dynamic cursor: Define a ref cursor, which means that the cursor can be opened dynamically by a set of static SQL statements. Define the type of the ref cursor first and then the cursor variable of this cursor type. Dynamically bind a SELECT statement through OPEN FOR when the cursor is opened.

    Figure 2 and Figure 3 show the syntax diagrams for defining a dynamic cursor.

    Figure 2 cursor_typename::=

    GaussDB(DWS) supports the dynamic cursor type sys_refcursor. A function or stored procedure can use the sys_refcursor parameter to pass on or pass out the cursor result set. A function can return sys_refcursor to return the cursor result set.

    Figure 3 dynamic_cursor_define::=

  2. Open the static cursor: Execute the SELECT statement corresponding to the cursor. The query result is placed in the work area and the pointer directs to the head of the work area to identify the cursor result set. If the cursor query statement contains the FOR UPDATE option, the OPEN statement locks the data row corresponding to the cursor result set in the database table.

    Figure 4 shows the syntax diagram for opening a static cursor.

    Figure 4 open_static_cursor::=

    Open the dynamic cursor: Use the OPEN FOR statement to open the dynamic cursor and the SQL statement is dynamically bound.

    Figure 5 shows the syntax diagram for opening a dynamic cursor.

    Figure 5 open_dynamic_cursor::=

    A PL/SQL program cannot use the OPEN statement to repeatedly open a cursor.

  3. Fetch cursor data: Retrieve data rows in the result set and place them in specified output variables.

    Figure 6 shows the syntax diagram for fetching cursor data.

    Figure 6 fetch_cursor::=

  4. Process the record.
  5. Continue to process until the active set has no record.
  6. Close the cursor: When fetching and finishing the data in the cursor result set, close the cursor immediately to release system resources used by the cursor and invalidate the work area of the cursor so that the FETCH statement cannot be used to fetch data any more. A closed cursor can be reopened using the OPEN statement.

    Figure 7 shows the syntax diagram for closing a cursor.

    Figure 7 close_cursor::=

Attributes

Cursor attributes are used to control program procedures or learn about program status. When a DML statement is executed, the PL/SQL opens a built-in cursor and processes its result. A cursor is a memory segment for maintaining query results. It is opened when a DML statement is executed and closed when the execution is finished. An explicit cursor has the following attributes:

  • %FOUND: Boolean attribute, which returns TRUE if the last fetch returns a row.
  • %NOTFOUND: Boolean attribute, which works opposite to the %FOUND attribute.
  • %ISOPEN: Boolean attribute, which returns TRUE if the cursor has been opened.
  • %ROWCOUNT: numeric attribute, which returns the number of records fetched from the cursor.

Examples