Alternatives to cursors in SQL Server

Tags:
Cursor
SQL Cursors
SQL Server
SQL Server Cursors
What are the alternatives to cursors? I can’t find any other method to move through record sets one at a time.
ASKED: April 23, 2009  5:49 PM
UPDATED: April 6, 2010  10:28 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

Answer
The main purpose that cursors fulfill is traversing of the row set. Cursors can be best avoided by:

a. Using the SQL while loop: Using a while loop we can insert the result set into the temporary table.

b. User defined functions: Cursors are sometimes used to perform some calculation on the resultant row set. This cam also be achieved by creating a user defined function to suit the needs

———-

A while loop will be just as slow as a cursor. Functions have there own performance issues to deal with.

Your best bet is to use a rowbased process instead. There are several cases when cursors are the best option.

If you can provide samples of what you are trying to do better options may be able to be provided.

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following