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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:07:57+00:00 2026-06-13T04:07:57+00:00

Problem: I need to loop through the records in one table, pulling the employee

  • 0

Problem:

I need to loop through the records in one table, pulling the employee number and comparing this employee number against another table to see if they’re still active employees. If they are no longer active employees I need to pass the data from this row into another stored proc.

Research:

I’ve googled around quite a bit and realize that I shouldn’t use cursors for this. I did however, find the following examples:

  1. http://ask.sqlservercentral.com/questions/7969/loop-through-records-in-a-temporary-table.html
  2. http://eedle.com/2010/11/11/looping-through-records-in-sql-server-stored-procedure/

However, it seems like they use a pk to loop through the records. Employee numbers can be the same for multiple recods in my scenario

Questions:

  1. Is it possible to achieve what I’m attempting without cursors?
  2. If it is possible, how would I go about fetching each row with a non unique column?
  • 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-06-13T04:07:58+00:00Added an answer on June 13, 2026 at 4:07 am

    Since you haven’t given us a full description of your situation, we cannot give a complete answer, however, in general, it’s Loops that you want to avoid in a set-based language like SQL and not Cursors per se (the problem with Cursosr is that they require the Loops).

    In your comments you provide a little bit more information in that you want to “loop through first table, compare to second table, and when the compare fails i delete records from first table. In essense I’m deleting recods from the first table of employees who are no longer with the company.“

    Here is how you can do this in SQL:

    DELETE  From FirstTable
    WHERE   FirstTable.EmployeeID NOT IN
        (
            SELECT  SecondTable.EmployeeID 
            FROM    SecondTable
            WHERE   SecondTable.Flag = 'Y'
        )
    

    No Loops are needed …


    If the issue then is that you want to use a pre-existing Stored Procedure to do the deletion, then there are a several possibilities:

    First, you can extract the contents of the Stored Procedure and re-write them for these preceding WHERE conditions. I understand that this is code duplication and that it violates some people’s DRY instincts, however, understand that SQL is NOT an Object-Oriented development environment and that sometimes code duplication has to happen.

    The second option would be to refactor the stored procedure so that it could accept a TableParameter for its EmployeeId’s to Delete. This is complicated though, and we would need to see that stored procedure to advise you on it.

    The third option would be to use string aggregation to build dynamic SQL to call the stored procedure for each EmployeeID to be deleted like so:

    DECLARE @sql As NVarchar(MAX);  
    SET     @sql = N'';
    
    SELECT  @sql = @sql + ' 
        EXEC YourProc ''' + CAST(EmployeeID As NVARCHAR(MAX)) + '''; '
    FROM    FirstTable
    WHERE   FirstTable.EmployeeID IN
        (
            SELECT  SecondTable.EmployeeID 
            FROM    SecondTable
            WHERE   SecondTable.Flag = 'Y'
        )
    
    EXEC(@sql);
    

    This avoids both the Looping and the Cusror problems, though many dislike it also. I prefer this solution myself, largely because of its generality.

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

Sidebar

Related Questions

my heading kinda explains the problem. I need to loop through multiple ul tags
I need to loop through a number (xx). xx always starts at zero. My
Can anyone please help. I have a problem where I need to loop through
Problem: I need to write/read objects from a file.This because I need to write/read
I need to loop through each pixel of a 2560x2160 2D numpy array (image).
I need to loop through a returned array in javascript but it doesn't work,
I need to loop through an array and for each element create a textfield.
I need to loop through Components and assign an event handler (for example Dynamically
This problem is driving me crazy, I'm sure I'm missing something. I need to
i have a collection of objects. i need to loop through the collection and

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.