A Cursor is basically a pointer to the Context Area. PL/SQL block controls the context area using a cursor. A cursor holds the one or more rows returned by a PL/SQL statement. Cursors and can be named as well as be kept anonymous. Named cursors can help for its reference so that it can be fetched in a program later on and process the rows returned by the SQL statement one by one.
In Oracle SQL, there are two types of cursors:
1. Implicit cursors 2. Explicit cursors
Implicit Cursors
The Implicit Cursor is the Default Cursor in PL/SQL blocks. These are created when there is no explicit cursor for the statement or when PL/SQL encounters a SELECT statement that returns just one row and also when Data Manipulation Language (DML) statements like DELETE, INSERT and UPDATE are encountered.
Some of the PL/SQL attributes that are used frequently are mentioned below:
- %ROWCOUNT: It Returns the number of rows influenced by an UPDATE, DELETE or an INSERT statement.
- %NOTFOUND: It Returns TRUE if an UPDATE, DELETE or an INSERT statement influenced zero rows or a SELECT INTO statement returned no rows. Else, it Returns a FALSE.
- %FOUND: It Returns TRUE if an UPDATE, DELETE or an INSERT statement influenced one or more rows or a SELECT INTO statement returned one or more rows. Else, it returns FALSE.
- %ISOPEN: It Returns FALSE for Implicit cursors as Oracle closes the PL/SQL cursor by default after processing its associated PL/SQL statement.
Explicit Cursors
Explicit cursors are used if you need to have better control over the Context Area via Cursor. They should be used when you are executing a SELECT statement query that will return more than one row. Cursors can process one record at a given point of time even though it stores more than one record. An explicit cursor is defined in the declaration section of the PL/SQL Block. Explicit Cursor and Implicit Cursor both have the same output, but accessing is different.
Syntax For An Explicit Cursor
|
1
2
3
|
Cursor Cursor-Name
IS
Select-Statement;
|
Processing an Explicit Cursor involves the following steps:
1. Initialize the Cursor: Initializing the Cursor also means the Declaration of the cursor in the Declaration Section of a PL/SQL Block. The name of the cursor requires to be defined along with the SELECT Statement.
Example
2. Open the Cursor: Opening the Cursor also means allocating the memory for the Cursor in the Context Area which thereby makes it sufficient to fetch and store records in it.
Example
|
1
|
OPEN admin_no;
|
3. Fetch the Cursor: Fetching the Cursor involves retrieval of data using the Fetch statement. It is used to help the Cursor process and access records or rows at a time.
Example
4. Close the Cursor: Once the work associated for a Cursor to be completed is accomplished, it is necessary to release the Allocated Memory of the Cursor to let other tasks occupy memory with any memory down-time.
Example
|
1
|
CLOSE Admin_no;
|
Program to write a Cursor to list all the Administrators in the Database Table named as Admin
游标基本上是指向上下文区域的指针。 PL / SQL块使用游标控制上下文区域。 游标保存由PL / SQL语句返回的一或多个行。 游标和可以命名,也可以保持匿名。 命名游标可以帮助其参考,以便稍后可以在程序中获取它,并一一处理SQL语句返回的行。
在Oracle SQL中,有两种类型的游标:
1.隐式游标2.显式游标
隐式游标
隐式游标是PL / SQL块中的默认游标。 当没有用于该语句的显式游标或PL / SQL遇到仅返回一行的SELECT语句,以及遇到诸如DELETE,INSERT和UPDATE的数据操作语言(DML)语句时,将创建它们。
下面提到一些常用的PL / SQL属性:
- %ROWCOUNT:它返回受UPDATE,DELETE或INSERT语句影响的行数。
- %NOTFOUND:如果UPDATE,DELETE或INSERT语句影响零行,或者SELECT INTO语句不返回任何行,则返回TRUE。 否则,它返回FALSE。
- %FOUND:如果UPDATE,DELETE或INSERT语句影响一个或多个行,或者SELECT INTO语句返回一个或多个行,则返回TRUE。 否则,它返回FALSE。
- %ISOPEN:当Oracle处理关联的PL / SQL语句后默认关闭PL / SQL游标时,它对隐式游标返回FALSE。
显式游标
如果需要通过游标更好地控制上下文区域,则使用显式游标。 当您执行将返回多个行的SELECT语句查询时,应使用它们。 游标即使可以存储多个记录,也可以在给定的时间点处理一个记录。 在PL / SQL块的声明部分中定义了一个显式游标。 显式游标和隐式游标都具有相同的输出,但是访问是不同的。
显式游标的语法
处理显式游标涉及以下步骤:
1.初始化游标:初始化游标还意味着在PL / SQL块的声明部分中声明游标。 游标的名称需要与SELECT语句一起定义。
例
|
1
|
CURSOR admin_no IS SELECT ano , aname from Admin ;
|
2.打开游标:打开游标还意味着在上下文区域中为游标分配内存,从而使其足以在其中存储和读取记录。
例
3.获取游标:获取游标涉及使用Fetch语句检索数据。 它用于帮助Cursor处理和一次访问记录或行。
例
|
1
2
3
|
FETCH a_admin
INTO
a_ano , a_aname ;
|
4.关闭游标:完成要完成的与游标相关的工作后,有必要释放游标的分配内存,以使其他任务在任何内存停机时间内占用内存。
例
编写一个游标以列出名为Admin的数据库表中所有管理员的程序
|
1
2
3
4
5
6
7
8
9
10
11
|
declare
CURSOR c1 IS
SELECT aname from Admin ;
rec c1 % rowtype ;
begin
for rec in c1
loop
dbms_output . put_line ( rec . aname ) ;
end loop ;
end ;
/
|
翻译自: https://www.thecrazyprogrammer.com/2015/07/plsql-cursors.html