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 228955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:43:32+00:00 2026-05-11T19:43:32+00:00

I’d like to know your experience(s) with replacing SQL Server cursors in existing code,

  • 0

I’d like to know your experience(s) with replacing SQL Server cursors in existing code, or how you took a problem that a procedural guy would use a cursor to solve, and did it set-based.

What was the problem the cursor was used to solve? How did you replace the cursor?

  • 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-11T19:43:32+00:00Added an answer on May 11, 2026 at 7:43 pm

    try to never loop, work on sets of data.

    you can insert, update, delete multiple rows at one time. here in an example insert of multiple rows:

    INSERT INTO YourTable
            (col1, col2, col3, col4)
        SELECT
            cola, colb+Colz, colc, @X
            FROM ....
                LEFT OUTER JOIN ...
            WHERE...
    

    When looking at a loop see what it done inside it. If it is just inserts/deletes/updates, re-write to use single commands. If there are IFs, see if those can be CASE statements or WHERE conditions on inserts/deletes/updates. If so, remove the loop and use set commands.

    I’ve taken loops and replaced them with the set based commands and reduced the execution time from minutes to a few seconds. I have taken procedures with many nested loops and procedure calls and kept the loops (was impossible to only use inserts/deletes/updates), but I removed the cursor, and have seen less locking/blocking and massive performance boosts as well. Here are two looping methods that are better than cursor loops…

    if you have to loop, over a set do something like this:

    --this looks up each row for every iteration
    DECLARE @msg VARCHAR(250)
    DECLARE @hostname sysname
    
    --first select of currsor free loop
    SELECT @hostname= min(RTRIM(hostname))
        FROM  master.dbo.sysprocesses (NOLOCK)
        WHERE  hostname <> ''
    
    WHILE @hostname is not null
    BEGIN
        set @msg='exec master.dbo.xp_cmdshell "net send ' 
            + RTRIM(@hostname) + ' '
            + 'testing  "'
        print @msg
        --EXEC (@msg)
    
        --next select of cursor free loop
        SELECT @hostname= min(RTRIM(hostname))
            FROM master.dbo.sysprocesses (NOLOCK)
            WHERE  hostname <> ''
            and hostname > @hostname
    END
    

    if you have a reasonable set of items (not 100,000) to loop over you can do this:

    --this will capture each Key to loop over
    DECLARE @msg VARCHAR(250)
    DECLARE @From   int
    DECLARE @To     int
    CREATE TABLE #Rows
    (
         RowID     int not null primary key identity(1,1)
        ,hostname  varchar(100)
    )
    
    INSERT INTO #Rows
    SELECT DISTINCT hostname
        FROM  master.dbo.sysprocesses (NOLOCK)
        WHERE  hostname <> ''
    SELECT @From=0,@To=@@ROWCOUNT
    
    WHILE @From<@To
    BEGIN
        SET @From=@From+1
    
        SELECT @msg='exec master.dbo.xp_cmdshell "net send ' 
            + RTRIM(hostname) + ' '
            + 'testing  "'
            FROM #Rows WHERE RowID=@From
        print @msg
        --EXEC (@msg)
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Simply select the <ul> and append the <li> to it… May 12, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer would something like xmoov work for you? it appears to… May 12, 2026 at 9:50 pm
  • Editorial Team
    Editorial Team added an answer The major answer here is 'it depends'. The general way… May 12, 2026 at 9:50 pm

Related Questions

I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have text I am displaying in SIlverlight that is coming from a CMS
I am currently running into a problem where an element is coming back from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.