Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:13:19+00:00 2026-05-27T08:13:19+00:00

Currently I have to work a bit more with SQL Server and I’ve read

  • 0

Currently I have to work a bit more with SQL Server and I’ve read about cursors and how you should try to avoid them, because they’re using a lot of resources and because they’re slow.

Now I’ve tried to do some basic stuff with the cursor and tried to rebuild it with a while loop. At the end the cursor was ~10x faster than the while loop, while resources were about 60% to 40%.

Maybe because I have a strange example exercise?

This is about the exercise I gave myself:

  • Iterate through a table, this table has exactly one column: “Names”, containing about 1000 names.
  • Print each name separately.

It’s pretty easy with the cursor, but for the while-loop I need a counter or something. As the table has no index, I can’t use that. So my solution was, that I created a temporary table, added all names to the table and include an index (or a row number). But the insert-operation takes about 95% of the time and at the end the while-loop is slower than the cursor.

Edit:
Another thing I tried was to use a WITH Names_Rows AS... and added Row_Number() as column and then used the while loop to iterate through the Names_Rows construct. But that took even longer.

Did I miss an easy way to iterate through a table using while without having an index?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:13:20+00:00Added an answer on May 27, 2026 at 8:13 am

    The reason for avoiding cursors is because you are performing imperative one row at a time operations rather than declarative set based operations.

    Replacing a cursor with a while loop will not in itself magically make the performance better and may well make performance worse as you have found out. The difference in performance between the two will depend upon your exact code / table structures and the cursor options you choose as covered in this series of articles.

    To print out all names from the table in a more set based manner you could do

    DECLARE @Names nvarchar(max);
    
    SELECT @Names = ISNULL(@Names + '
    ','') + Names
    FROM YourTable;
    
    PRINT @Names;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have a class, TransactionData , which is just a little bit more
The university I work at uses Oracle for the database system. We currently have
I currently work in the position of Data Warehouse programmer and as such have
I currently work for a bespoke software agency. Does anyone have any experience of
I'm currently having difficulty finding a way of making my loops work. I have
I have a simple SQL query (SQL Server 2005) where I'm selecting from a
We have an app that creates queues and services in SQL Server using the
I have a simple database table (SQL Server 2008 R2 Express), which has a
Currently I want to optimize my 3d engine for consoles a bit. More precisely
Anything I have tried didn't work. Currenly I have following code to change asp.net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.