Alternative to use cursors in SQL Server stored procedure

SQL Server, like other relational databases, is desgined to, and is pretty good at, working on sets of data.

Databases are not good at procedural code where all the opportunities for optimization are obscured from the query processing engine.

Using RawStatusFeed to store some proprietry request string and then processing a list of those one by one, is going to be ineffiencnt for database code. This might make the inserts very fast for the client, and this might be very important, but it comes at a cost.

If you break the request string down on insert, or better still, before insert via a specialised SP call, then you can store the required changes in some intermediate relational model, rather than a list of strings. Then, every so often, you can process all the changes at once with one call to a stored procedure. Admittedly, it would probably make sense for that stored procedure to contain several query statements. However, with the right indexes and statistics the query processing engine will able to make an efficient execution plan for this new stored procedure.

The exact details of how this should be achieved depend on the exact details of the RawStatusFeed table and the implementation of usp_pushRawDataAndProcess. Although this seems like a rewrite, I don't imagine the DeviceCode column is that complicated.


So, the short answer is certainly yes but, I'd need to know what usp_pushRawDataAndProcess does in detail.

The signature of the usp_pushRawDataAndProcess SP is acting as a bottle neck.


If you can't change usp_pushRawDataAndProcess and and won't create a set based alternative then you are stuck with the bottle neck.

So, rather than removing the bottle neck you could take another tack. Why not make more concurrent instances of the bottle neck to feed the data through.

If you are using SQL Server 2005 or above you could use some CLR to perform numerous instances of usp_pushRawDataAndProcess in parallel.

Here is a link to a project I used before to do something similar.


I had always disliked cursors because of their slow performance. However, I found I didn't fully understand the different types of cursors and that in certain instances, cursors are a viable solution.

When you have a business problem that can only be solved by processing one row at a time, then a cursor is appropriate.

So to improve performance with the cursor, change the type of cursor you are using. Something I didn't know was, if you don't specify which type of cursor you are declaring, you get the Dynamic Optimistic type by default, which is the one that is the slowest for performance because it's doing lots of work under the hood. However, by declaring your cursor as a different type, say a static cursor, it has very good performance.

See these articles for a fuller explanation:

The Truth About Cursors: Part I

The Truth About Cursors: Part II

The Truth About Cursors: Part III

I think the biggest con against cursors is performance, however, not laying out a task in a set based approach would probably rank second. Third would be readability and layout of the tasks as they usually don't have a lot of helpful comments.

The best alternative to a cursor I've found is to rework the logic to take a set based approach.

SQL Server is optimized to run the set based approach. You write the query to return a result set of data, like a join on tables for example, but the SQL Server execution engine determines which join to use: Merge Join, Nested Loop Join, or Hash Join. SQL Server determines the best possible joining algorithm based upon the participating columns, data volume, indexing structure, and the set of values in the participating columns. So it generally the best approach in performance over the procedural cursor approach.

Here is an article on Cursors and how to avoid them. It also discusses the alternatives to cursors.