Oracle explicit cursors use basic methods ____oracle

Source: Internet
Author: User
Tags oracle cursor

In Oracle, cursors are the most common and most efficient way to manipulate data.
Depending on the declaration method of an Oracle cursor, Oracle cursors can be divided into both explicit and implicit.
An explicit cursor is an explicitly declared cursor, and the operation of an explicit cursor is mainly in the following categories:

First, the declaration of the cursor:
When declaring a cursor, use the keyword CURSOR,
such as CURSOR Cur_sample is SELECT * from TAB;
Where, CURSOR, is the keyword, cur_sample is the cursor name, and the following SELECT statement is the query statement associated with the cursor. These parts must not be less.

Second, the parameter passes:
When declaring a cursor, you can use not only the preceding method:
CURSOR Cur_sample is SELECT * from TAB;
You can also pass parameters to the cursor,
CURSOR cur_sample (Par_tablename VARCHAR2) is a SELECT * from TAB WHERE tname = par_tablename;
The parameters passed to the cursor can be multiple or different types. However, parameters can only be passed in and cannot be transmitted.
You can set the default values at the same time as you specify parameters.
such as: CURSOR cur_sample (Par_tablename VARCHAR2 () DEFAULT ' EMPLOYEES ') is the SELECT * from TAB WHERE tname = par_tablename;

Third, declaring a cursor variable
Declare a cursor in the following way:
CURSOR Cur_sample is SELECT * from TAB;
However, when using a cursor, a required segment declares a cursor variable or other variable that holds the data taken from the cursor:
There are two types of cursor variables:

(1) Define the variables for the cursor type:
C_smaple Cur_sample%rowtype;
(2)
Declare a list of variables:
M_tname VARCHAR2 (30);
M_tabtype VARCHAR2 (7);
M_clusterid number (5);

Four, open the cursor
Before you manipulate data, you first open the cursor:
Declare a cursor first:
CURSOR Cur_sample is SELECT * from TAB;
And then open again:
OPEN cur_sample;
For cursors with parameters, the arguments must be passed at the same time when opened:
If the cursor is declared as follows:
CURSOR cur_sample (Par_tablename VARCHAR2 () DEFAULT ' EMPLOYEES ') is the SELECT * from TAB WHERE tname = par_tablename;
Open the method as follows:
OPEN cur_sample (' EMPLOYEES ');
However, if the parameter has a default value and you want to use the default value, you can directly:
OPEN cur_sample;
At this point, the Par_tablename value equals ' EMPLOYEES ', which is equivalent
OPEN cur_sample (' EMPLOYEES ');

However, if you open a cursor that is already open, an error occurs:
Ora-06511:pl/sql:cursor already open;

When a cursor is OPEN, the cursor's pointer automatically points to the first record of the active set.

Five, read cursor data:
Before reading data, the cursor must be opened first:
You need to pass arguments, open them with arguments, and do not require arguments.

Reading the data means fetching the data into the variable using the Fetch method.
For a variable of a cursor type, you can use the following method: (see the third for the definition of the cursor parameter)
FETCH cur_sample into C_sample;
can also
FETCH Cur_smale into M_tname,m_tabtype,m_clusterid;
When you use a list of variables, the order of the list of variables must be the same as the order of the fields in the cursor activity result set.

If you use a variable of a cursor type, you access its value in the following ways:
C_smaple. Tname,c_sample. Tabtype,c_sample. Clusterid;
For a method with a list of variables, you can use the variable name directly.

Once the FETCH is performed, the cursor pointer automatically moves to the next record.

Six, close the cursor:
Turning off cursors is the simplest, direct
The close cursor name; It's OK.
As for cursors:
CURSOR Cur_sample is SELECT * from TAB;
You can:
Close cur_sample;
The cursor has parameters and the Close method is the same.

The use of cursors can greatly facilitate pl/sql writing, but the cursor is also special, such as cursor data can only be read by fetch, cursor pointer movement in the fetch data, automatically moved to the next, when the cursor is opened, the cursor pointer automatically points to the first record, in addition, the cursor is opened in a static way, That is, after the cursor is opened, if the related table performs a DML (DELETE, INSERT, update) operation, the result of the operation is not reflected in the cursor, only the cursor is closed and the cursor can be opened before the cursor activity set is updated.

Related Article

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.