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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:46:55+00:00 2026-05-17T01:46:55+00:00

I want to read EmpID in EMP Table based on some condition. For every

  • 0

I want to read EmpID in EMP Table based on some condition. For every EmpID I need to do some operation in another table. How can I read single value of EmpID at a time.

Thanks in advance

  • 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-17T01:46:56+00:00Added an answer on May 17, 2026 at 1:46 am

    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...
    

    you can even insert into multiple tables in a single statement:

    INSERT INTO YourTable
            (col1, col2, col3, col4)
            OUTPUT INSERTED.PK, Inserted.Col2
                INTO OtherTable (ColA, ColB)
        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
        --just some example of some odd task that requires a loop
        set @msg='exec master.dbo.xp_cmdshell "net send ' 
            + RTRIM(@hostname) + ' '
            + 'testing  "'
        print @msg
        --EXEC (@msg) --<<will not actually send the messages
    
        --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  --use a table @variable depending on the number of rows to handle
    (
         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
    
        --just some example of some odd task that requires a loop
        SELECT @msg='exec master.dbo.xp_cmdshell "net send ' 
            + RTRIM(hostname) + ' '
            + 'testing  "'
            FROM #Rows 
            WHERE RowID=@From
        print @msg
        --EXEC (@msg) --<<will not actually send the messages
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.