9. Cursor-Configuration Properties
The cursor-configuration properties, as shown in Table 8, are used to control cursor behavior in SQL Server.
Table 8 Cursor-Configuration Properties
Tip
To view information about the open cursors in various databases, query the sys.dm_exec_cursors dynamic management view.
Cursor Threshold
The cursor threshold
property sets the number of rows in a cursor set before the cursor
keysets are asynchronously generated. The query optimizer estimates the
number of rows that will be returned from the result set. If the
estimated number of rows is greater than the cursor threshold,
then the cursor is asynchronously generated; if it is synchronously
generated causing a delay, the query must wait until all rows are
fetched. Every cursor keyset will be asynchronously generated if the cursor threshold property is set to 0.
The default of -1
causes all keysets to be synchronously generated, which is okay for
smaller keysets. For larger cursor keysets, though, this may be a
problem.
In Management Studio, you can set the cursor threshold option to the wanted value in the “Cursor Threshold box on the Server Properties Advanced tab (refer to Figure 6).
When you work with cursors, the following code permits synchronous cursor keysets for cursors of up to 10,000 rows:
EXEC sp_configure ‘show advanced options', 1;
RECONFIGURE;
EXEC sp_configure ‘cursor threshold', 10000;
RECONFIGURE;
Cursor Close on Commit
The cursor close on commit property closes an open cursor after a transaction is committed when set to on. If it is set to off (default), then cursors remain open across transactions until a close cursor statement is issued.
The cursor close on commit option can be set from Management Studio and code at server, database, and connection level. In Management Studio, the cursor close on commit option can be turned on at the following level:
- Server level: Checks the Cursor Close on Commit check box on the Server properties Connections tab (refer to Figure 5).
- Database level: Selects True for the Close Cursor on Commit Enabled box on the Database Properties Options tab .
- Connection level: To set this property for current queries,
click the Query menu ? Query Options ? Execution ? ANSI, and check the
Set Cursor Close on Commit check box. To set this property for all
future connections, click the Tools menu ? Options ? Query Execution ?
ANSI, and check Set Cursor Close on Commit check box
To set cursor close on commit in code, do the following:
- Server level: Sets the option on for the server:
EXEC sp_configure ‘user options', 4;
RECONFIGURE;
- Database level: Sets the option on for the AdventureWorks2012 sample database:
ALTER DATABASE AdventureWorks2012 SET CURSOR_CLOSE_ON_COMMIT ON;
- Connection level: Sets the option on for the current connection:
SET CURSOR_CLOSE_ON_COMMIT ON;
Cursor Default
This property makes each cursor local to the object that declared it when set to local. When it is set to global (default), the scope of the cursor can be extended outside the object that created it.
In Management Studio, you can set the cursor default option to the wanted scope in the Default Cursor box on the Database Properties Options tab .To set the cursor default for the AdventureWorks2012 sample database to LOCAL in code, do the following:
ALTER DATABASE AdventureWorks2012 SET CURSOR_DEFAULT LOCAL;