All of my Hitchhiker's Guide books have discussed cursors at lengthat least the ones where ADO, RDO, or DAO implemented them. Since ADO.NET does not support this paradigm, I'll need to add a few paragraphs here to refresh your understanding of the mechanics and terminology. First, when you execute a query to build a server-side cursor (or any cursor, for that matter), SQL Server looks through the database to find qualifying rows. Your WHERE clause and other SELECT statement operators determine which rows are to be included and which are not. While this operation is not instantaneous, the rows found become part of the cursor "membership." If there is no WHERE clause in the SELECT query, all rows in the target table(s) are made members of the cursorthat can be expensive. Remember that server-side cursors (like a keyset cursor) are simply a temporary collection of pointers to the actual rows. There are several types of server-side cursors[1]:
Other cursor options permit you to specify if the cursor can be "seen" by other applications accessing SQL Serverbeyond your application's connection. See SQL Server 2005 BOL for more specific details on these options. They describe how to make cursors visible to stored procedures and elsewhere. What Do Server-Side Cursors Cost?One of the downsides of server-side cursors is the burden they impose on T-SQL Server and the scalability of your applications. When you create a server-side cursor, T-SQL Server writes a set of keys or entire rows to memory or TempDB. This process is repeated and its effects multiplied for each application that opens a server-side cursor. Using server-side cursors also assumes that the connection remains open as long as the cursor's data is needed. This is definitely not a good idea for a Web-based (ASP/ASPX) application because the connection is usually closed, and its resources released, just after its queries are executed. On the other hand, for many traditional client/server applications, server-side cursors have been an effective way to manage data and simplify application designwhen used wisely. |