Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
Hi,

I want to know which is the best alternative for Cursor in SQL. Since VB 6.0, I am using Loop coding in BL/DL and rarely Cursor for row wise database operations. But I want to know what is the best alternative for cursor because it has many disadvantages. I was searching on Google and found options like WHILE LOOP, TEMP Tables, User defined functions, etc., but I want opinions from CP people too.

[Note: Now I'm using WHILE LOOP.]

Thank you.
thatraja
Posted
Updated 13-Feb-10 18:04pm
v4

RDBMS are designed for set based logic. Anytime update, select, transform, insert, and delete that you can perform with a set operation will be infinitely faster than the cursor option.

In SQL Server the use of a cursor is often hidden so let me show a quick tip to know when you have a cursor. Anytime you have a loop on data you have a cursor. When you load the data into code, you have to use a cursor to do so. Thus a while loop in C# on data did at one point use a cursor in SQL Server, always.

And before you ask, an overview of what can be accomplished in set based operations is well beyond the scope of a quick question, or even a CP article as the topic spans several upper level university courses.
 
Share this answer
 
I think it depends on what you want to do. I use while loops and user defined functions
 
Share this answer
 
Your question is a little confusing because you are talking about a cursor and then about VB6. I guess you are opening a cursor on SQL Server from VB6. I generally don't like this approach (as you said) - but especially if you are connecting your cursor to a non temporary table.

If you have the chance to redesign the database and its interactions, then I would strongly suggest moving as much logic into stored procedures as possible and so avoid cursors in VB6 that way.

If you do not have this luxury, then I would suggest you use cursors or multiple insert statements to load up a temporary table and then use TSQL to use the temporary table to insert/update the permanent one. The idea is to get all long running activities off permanent tables by either converting them into lots of little actions or making them happen faster.

The best performance I have seen using SQL Server and VB6 is via using disconnected record sets for reads (the whole record set is loaded into the client and then when you loop over it you are not using a cursor). Then separate multiple inserts are used to communicate the other way. This might seem the counter intuitive, but it actually gives the RDBMS the best chance to optimize its self.
 
Share this answer
 
The best alternative to Cursor is a Select statement.

Cursor actually creates a temporary table to loop through the data. So if you are creating a temporary table yourself, then it is no good for you.

So manage the datamanipulation directly from the SQL.
:thumbsup:
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900