Summary: in this tutorial, you will learn about PL/SQL cursor and its usage.
A cursor is a pointer that points to a result of a query. PL/SQL has two types of cursors: implicit cursors and explicit cursors.
Implicit cursors
Whenever Oracle executes an SQL statement such as SELECT INTO
, INSERT
, UPDATE
, and DELETE
, it automatically creates an implicit cursor.
Oracle internally manages the whole execution cycle of implicit cursors and reveals only the cursor’s information and statuses such as SQL%ROWCOUNT
, SQL%ISOPEN
, SQL%FOUND
, and SQL%NOTFOUND
.
The implicit cursor is not elegant when the query returns zero or multiple rows which cause NO_DATA_FOUND
or TOO_MANY_ROWS
exception respectively.
Explicit cursors
An explicit cursor is an SELECT
statement declared explicitly in the declaration section of the current block or a package specification. For an explicit cursor, you have control over its execution cycle from OPEN
, FETCH
, and CLOSE
.
Oracle defines an execution cycle to execute an SQL statement and associates a cursor with it. The following illustration shows the execution cycle of an explicit cursor:
- for in loop形式
1 | DECLARE |
2.普通的游标循环
1 | declare |
3.高效的游标循环
1 | declare |
BULK COLLECT 子句会批量检索结果,即一次性将结果集绑定到一个集合变量中,并从SQL引擎发送到PL/SQL引擎。通常可以在SELECT INTO、
FETCH INTO以及RETURNING INTO子句中使用BULK COLLECT.
BULK COLLECT的限制
1、不能对使用字符串类型作键的关联数组使用BULK COLLECT 子句。
2、只能在服务器端的程序中使用BULK COLLECT,如果在客户端使用,就会产生一个不支持这个特性的错误。
3、BULK COLLECT INTO 的目标对象必须是集合类型。
4、复合目标(如对象类型)不能在RETURNING INTO 子句中使用。
5、如果有多个隐式的数据类型转换的情况存在,多重复合目标就不能在BULK COLLECT INTO 子句中使用。
6、如果有一个隐式的数据类型转换,复合目标的集合(如对象类型集合)就不能用于BULK COLLECTINTO 子句中。
Links: