依照游标的功能来分,可以分为四种。
静态游标 (Static Cursor) 是利用暂存资料表作为储存结果集空间的一种游标,它可以让应用程式可以快速的存取结果集,但在静态游标开启期间,任何对资料表所做的变更都不会反映在结果集中;同时,在静态游标中所作的修改,无法反映到资料库中,此种游标是消耗资源度第三的游标。
Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in tempdb; therefore, modifications made to base tables are not reflected in the data returned by fetches made to this cursor, and this cursor does not allow modifications.
动态游标 (Dynamic Cursor) 是可以反映资料库中修改的一种游标,不过它并不会让结果集的位置固定 (随机变动),因此无法确实的以游标位置来判断资料,并且它因为要随时反映资料库的变化,因此伺服器需要消耗较多的资源,此种游标是消耗资源第二高的游标。
Defines a cursor that reflects all data changes made to the rows in its result set as you scroll around the cursor. The data values, order, and membership of the rows can change on each fetch. The ABSOLUTE fetch option is not supported with dynamic cursors.
索引键集游标 (Keyset Cursor) 是动态游标的强化版本,借由维护一个资料集位置对应表 (以 SQL Server 为例,会建立在 tempdb 的 keyset 资料表中),以维护在结果集中的顺序不受更新而变化,但这相对的也付出了伺服器效能和资源消耗的代价,因此索引键集游标是最消耗伺服器资源的一种游标,在实务上应避免使用。
仅前移型游标 (Forward-Only Cursor) 是一旦将游标往前移时,其走过的游标之前的结果集就会被舍弃,因此应用程式不能再往后移动游标,但也因此让伺服器只需要记住游标在结果集中目前的位置即可,这让它消耗的资源只有游标而已,是最省资源的一种游标,在实务中被广泛使用,像 ADO.NET 的 DataReader 就只限定只能使用 Forward-Only Cursor。
Specifies that the cursor can only be scrolled from the first to the last row. FETCH NEXT is the only supported fetch option. If FORWARD_ONLY is specified without the STATIC, KEYSET, or DYNAMIC keywords, the cursor operates as a DYNAMIC cursor. When neither FORWARD_ONLY nor SCROLL is specified, FORWARD_ONLY is the default, unless the keywords STATIC, KEYSET, or DYNAMIC are specified. STATIC, KEYSET, and DYNAMIC cursors default to SCROLL. Unlike database APIs such as ODBC and ADO, FORWARD_ONLY is supported with STATIC, KEYSET, and DYNAMIC Transact-SQL cursors.