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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:18:47+00:00 2026-05-10T18:18:47+00:00

I know that cursors are frowned upon and I try to avoid their use

  • 0

I know that cursors are frowned upon and I try to avoid their use as much as possible, but there may be some legitimate reasons to use them. I have one and I am trying to use a pair of cursors: one for the primary table and one for the secondary table. The primary table cursor iterates through the primary table in an outer loop. the secondary table cursor iterates through the secondary table in the inner loop. The problem is, that the primary table cursor though apparently proceeding and saving the primary key column value [Fname] into a local variable @Fname, but it does not get the row for the corresponding foreign key column in the secondary table. For the secondary table it always returns the rows whose foreign key column value matches the primary key column value of the first row of the primary table.

Following is a very simplified example for what I want to do in the real stored procedure. Names is the primary table

SET NOCOUNT ON DECLARE      @Fname varchar(50) -- to hold the fname column value from outer cursor loop     ,@FK_Fname varchar(50) -- to hold the fname column value from inner cursor loop     ,@score int ;  --prepare primary table to be iterated in the  outer loop DECLARE @Names AS Table (Fname varchar(50)) INSERT @Names     SELECT 'Jim' UNION     SELECT 'Bob' UNION     SELECT 'Sam' UNION     SELECT 'Jo'    --prepare secondary/detail table to be iterated in the inner loop DECLARE @Scores AS Table (Fname varchar(50), Score int) INSERT @Scores     SELECT 'Jo',1 UNION     SELECT 'Jo',5 UNION     SELECT 'Jim',4 UNION     SELECT 'Bob',10 UNION     SELECT 'Bob',15   --cursor to iterate on the primary table in the outer loop DECLARE curNames CURSOR FOR SELECT Fname FROM @Names   OPEN curNames FETCH NEXT FROM curNames INTO @Fname  --cursor to iterate on the secondary table in the inner loop DECLARE curScores CURSOR FOR      SELECT FName,Score      FROM @Scores      WHERE Fname = @Fname   --*** NOTE: Using the primary table's column value @Fname from the outer loop  WHILE @@FETCH_STATUS = 0 BEGIN     PRINT 'Outer loop @Fname = ' + @Fname      OPEN curScores     FETCH NEXT FROM curScores INTO @FK_Fname, @Score      WHILE @@FETCH_STATUS = 0     BEGIN         PRINT ' FK_Fname=' + @FK_Fname + '. Score=' + STR(@Score)         FETCH NEXT FROM curScores INTO @FK_Fname, @Score     END     CLOSE curScores     FETCH NEXT FROM curNames INTO @Fname END  DEALLOCATE curScores  CLOSE curNames DEALLOCATE curNames 

Here is what I get for the result. Please note that for the outer loop it DOES show the up-to-date Fname, but when that Fname is used as @Fname to fetch the relevant row from the secondary table for the succeeding iterations, it still get the rows that match the first row (Bob) of the primary table.

Outer loop @Fname = Bob     FK_Fname=Bob. Score=10     FK_Fname=Bob. Score=15 Outer loop @Fname = Jim     FK_Fname=Bob. Score=10     FK_Fname=Bob. Score=15 Outer loop @Fname = Jo     FK_Fname=Bob. Score=10     FK_Fname=Bob. Score=15 Outer loop @Fname = Sam     FK_Fname=Bob. Score=10     FK_Fname=Bob. Score=15 

Please let me know what am I do wrong. 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. 2026-05-10T18:18:48+00:00Added an answer on May 10, 2026 at 6:18 pm

    The value of @fName is evaluated at :DECLARE curScores CURSOR and not in the primary loop. You must Declare and then deallocate the secon cursor in the primary loop.

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

Sidebar

Related Questions

I know that it's a subject that can raise a lot of debate, but
We know that behind the scenes, the ASP.NET MVC framework will use reflection to
I know that I should put all the html elements in body tag, but
I know that it's possible to implicitly provide asynchronous interaction using: Asynchronous Delegates Asynchronous
I know that this has already been asked here but the answer (using a
I know that the I can get a set of cursor files in C:/Windows/Cursors.
It is generally accepted that the use of cursors in stored procedures should be
I would like to know if it's possible to paginate using cursors and keep
Firefox says they support 128px cursors, but that you must keep within 32x32px for
I know that one of the differences between classes and structs is that struct

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.