Introduction of the concept of cursors (implicit cursors are commonly used, but only an internal mechanism)

Source: Internet
Author: User

There are two types of cursors: implicit and explicit. PL/SQL declares an implicit cursor for all SQL data operations, including query operations that only return one record..

In fact, Oracle's implicit cursor is used for every query operation in PL/SQL.

 

In addition, we can use a cursor to process a query result explicitly:

Declare
Cursor
C1Is
Select
Empno, ename, job
FromEMP
WhereDeptno = 20;

 

A row set returned by a multi-row query is called a result set ). Its size is the number of rows that meet our query conditions. As shown in, an explicit cursor "points to" the record of the current row, which allows our program to process a record each time.

 

In fact, a cursor is equivalent to a pointer pointing to a result set. It occupies possession of multiple field records for storage and processing.

Great advantages.

 

Multi-line query is processed like a file. For example, a COBOL program opens a file, processes records, and closes the file. Similarly, a PL/SQL program opens a cursor, processes the queried rows, and closes the cursor. Just as the file pointer can mark the current position in the open file, the cursor can mark the current position of the structure set.

We can use the open, fetch, and close statements to control the cursor. Open is used to open the cursor and point the cursor to the first row of the result set, fetch retrieves information about the current row and moves the cursor to the next row. When the last row is also processed, close closes the cursor.

 

Cursor For Loop

In most cases, explicit cursors can be used to replace open, fetch, and close statements with a simple for loop cursor. First, the cursor for loop implicitly declares a loop index (Loop
Index) variable. Next, it will open the cursor, repeatedly retrieve data from the result set, and place the data in various fields of the cyclic index. When all rows are processed, the cursor is closed. In the following example, the cursor for loop implicitly declares an emp_rec record:

 

Declare
Cursor
C1Is
Select
Ename, Sal, hiredate, deptno
FromEMP;
...
Begin
For
Emp_recInC1Loop
...
Salary_total: = salary_total + emp_rec.sal;
End Loop;
End;

 

To use every field in each queried row, we can use dot notation, which acts like a domain selector.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.