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

  • Home
  • SEARCH
  • 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 840527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:38:16+00:00 2026-05-15T05:38:16+00:00

I know how to accomplish my task with .NET, but I wanted to do

  • 0

I know how to accomplish my task with .NET, but I wanted to do this just in SQL.

I need to loop through all of the rows where the primary key is somewhat arbitrary. It can be a number or a series of letters, and probably any number of unusual things.

I know I could do something like this…

DECLARE @numRows INT
SET @numRows = (SELECT COUNT(pkField) FROM myTable)

DECLARE @I INT SET @I = 1 WHILE (@I <= @numRows) BEGIN --Do what I need to here SET @I = @I + 1 END

...if my rows were indexed in a contiguous fashion, but I don't know enough about SQL to do that if they're not. I keep coming across the use of "cursors," but I come across just as much reading about avoiding cursors.

I found this SO solution but I'm not sure if that's what I'm needing?

I appreciate any ideas.

  • 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-15T05:38:16+00:00Added an answer on May 15, 2026 at 5:38 am

    If you are going to loop through rows, use a cursor. it is much less efficient to do it the way you have shown. Basically what you are doing is a cursor without using SQL Server’s cursor facilities. Just because you don’t use the keyword CURSOR doesn’t mean it isn’t a cursor. Logically that’s what you have. Each of your selects is basically going to be a query on the underlying table. Especially if the table is not indexed, it may be much less efficient than a cursor. There are things you can do to make a cursor less bad…FORWARD_ONLY, etc. Also sometimes making a static cursor will make things faster, other times it will make things slower. Read up on the CURSOR statement.

    The reality is to avoid cursors you don’t just invent a cursor using select statements, that is stupidity and the people who do that should be fired. People who claim that this is not a cursor are deluding themselves… You need to re-think your entire problem. There are some things that cannot be done without cursors and for those, there are cursors. Basically I would define a cursor as any mechanism that performs an operation row by row on a table.

    Anyway the missing piece for what you are trying to do is that you can turn a non numeric key into a numeric key by creating a mapping table:

    SELECT IDENTITY(BIGINT, 1, 1) AS numKey, pkField INTO #keymap
    FROM MyTable
    
    DECLARE @I INT
    SET @I = 1
    WHILE (@I <= @numRows)
    BEGIN
        SELECT mt.*
        FROM  myTable mt inner join #keymap km on km.pkfield = mt.pkfield
        WHERE numkey = @I
        --Do what I need to here
        SET @I = @I  + 1
    END
    

    This is still very bad because now you are doing a join on every record…it may be slightly better to:

    A) SELECT IDENTITY(BIGINT, 1, 1) AS numKey, * INTO #keymap
    FROM MyTable
    Now #keymap is the same table as your original table but with a numeric key, so the solution you have will work without a join…however if the table is big or you want to update it, then this is not good

    B) instead of doing a join

    SELECT IDENTITY(BIGINT, 1, 1) AS numKey, pkField INTO #keymap
    FROM MyTable
    
    DECLARE @I INT, @myKey VARCHAR(255) -- assume varchar
    SET @I = 1
    WHILE (@I <= @numRows)
    BEGIN
        SELECT @myKey = pkfield FROM #keymap WHERE numKey = @i
        SELECT mt.*
        FROM  myTable
        WHERE pkfield = @myKey
        --Do what I need to here
        SET @I = @I  + 1
    END
    

    This way there will be no join, but you are now doing two queries…you may even need to index #keymap….

    The best thing is to re-evaluate your problem. Sometimes you can use CASE WHEN or an identity column to enable you to avoid the cursor. It really depends on the problem. But for those cases when you need to use a cursor, using an actual CURSOR is usually much faster than the other method. Even faster is to write a program in .NET, Java, even Perl/Python. I had a function with SQL Server 2000 that used two nested cursors and took over an hour before I gave up. When I wrote a Perl program to do the same thing it only took 10 minutes. And Perl, like any interpreted language, is much slower than a compiled language. Java/C/C++/C# would probably be 10/20/30 times quicker…. So if you really need a cursor, it is probably better to do it in another programming language. Especially if you are using SQL Server 2005 which has the CLR (Common Language Runtime) embedded in it…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I could use pagination gems to accomplish a task like this, but
Does anyone know about a good way to accomplish this task? Currently i'm doing
I'm curious to know how to accomplish this task. I have a function in
I don't know how to accomplish this! how to get the function pointer in
Know this might be rather basic, but I been trying to figure out how
I know that this sort of question has been asked here before, but still
This is a question about asp.net WebForms and sessionstate. I know this is probably
I know that there has been a lot of questions like this but I
Just wanted to check to see if there's a more elegant way to accomplish
That's a simple task in ASP.NET WebForms:If you want to show/hide a control all

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.