cursor is oracle 日期_Oracle SQL的cursor理解

分析一下,游标C1是显式定义,可以看作是一个查询语句的变量。其实它是指向这个查询语句得到的内存区域的指针。

在打开游标时,就是定位指针在查询结果集内存区域的位置,开始还是空值。使用fetch into操作依次移动指针定义结果集每一行记录,直到C1%NOTFOUND属性为true而退出循环。C1%NOTFOUND表示游标C1已经到最后一行了,不能再向下移动,无法找到更多的记录了。游标最后关闭,所有的显式游标再打开后都要关闭。

另外再深入说明一下,这个结果集不是真正意义上的结果集内存缓存,它还是从各个相关数据块中查询出来的。每一次执行,就遍历一次各个数据块。所以你会在执行统计结果中看到稳定的内存读数字。所以,这个结果集也是一个抽象的逻辑概念。(注,这段是个人理解,未必准确。)

使用隐式游标多数发生在update,delete ,insert操作中,它操作的也是一个结果集。如我们经常需要知道update某个表的记录数的结果。使用隐式游标的SQL%ROWCOUNT功能可能非常快捷地知道它。

setserveroutputon

declare

begin

updateall_objsetcreated = sysdatewhereobject_type ='TABLE';

dbms_output.put_line( ' 更新的记录数 :'|| sql % rowcount );

commit;

deletefromall_objwhereobject_type ='CLUSTER';

dbms_output.put_line( ' 删除的记录数 :'|| sql % rowcount );

commit;

insertintoall_objselect*fromall_objwhereobject_type ='TABLE';

dbms_output.put_line( ' 插入的记录数 :'|| sql % rowcount );

commit;

end;

/

执行结果如下:

更新的记录数 :11940

删除的记录数 :11

插入的记录数 :11940

PL/SQL procedure successfully completed

使用SQL隐式游标可以得到要update,delete,insert这些操作过程中结果集的记录数。隐式游标不需要开发打开和关闭。这些操作也是在一个结果集上进行操作,使用游标遍历结果集中的每一行记录。这样,游标就可以为一个指针。

看一下别人对游标的定义吧。

Oracle官方文档Oracle® Database Concepts 11g Release 2中这么说的:

A cursor is a name or  handle to a specific private SQL area.

游标是一个指向私有SQL区域的名字或者句柄。因为游标和私有SQL区域紧密相关,这两个名词有时候互换使用。

在开发过程中看到的游标指向的结果集,其实是私有SQL区域中语句执行的结果集。游标仅仅是这个结果集生成的方法。

私有SQL区域在专用服务模式下位于PGA中。PGA的组成分别为SQL work areas 和session memory 及private SQL area。

A work area is a private allocation of PGA memory used for memory-intensive operations. For example, a sort operator uses the sort area to sort a set of rows. Similarly, a hash join operator uses a hash area to build a hash table from its left input, whereas a bitmap merge uses the bitmap merge area to merge data retrieves from scans of multiple bitmap indexes.

The UGA is session memory, which is memory allocated for session variables, such as logon information, and other information required by a database session.

A private SQL area holds information about a parsed SQL statement and other session-specific information for processing. When a server process executes SQL or PL/SQL code, the process uses the private SQL area to store bind variable values, query execution state information, and query execution work areas.

私有SQL区域持有一个分析过的SQL语句和其他用于进程处理的会话指定的信息。当一个服务器进程执行一个SQL或PL/SQL代码事,这个进程使用私有SQL区域去保存绑定变量值,查询状态信息,和查询工作区域。

私有SQL区域分成下列两个区域:

运行时区域(run-time area)

This area contains query execution state information. For example, the run-time area tracks the number of rows retrieved so far in a full table scan.

Oracle Database creates the run-time area as the first step of an execute request. For DML statements, the run-time area is freed when the SQL statement is closed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值