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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:11:06+00:00 2026-05-22T21:11:06+00:00

I have a parent-child relationship in the database. What I need to do is

  • 0

I have a parent-child relationship in the database. What I need to do is loop through the parent’s query, and using the parent’s primary key, got get its children. The issue I am having is that I need to use a parameterized cursor (pass in the key) to do this.

Is there such a thing in SQL Server or a trick to mimic this? I tried doing this, but it didn’t work:

DECLARE @value   VARCHAR(20);
DECLARE @someKey NUMERIC(19,0);

DECLARE main_curs 
CURSOR FOR SELECT value FROM someTable where key = @someKey;

SET @someKey = 12345;

OPEN main_curs
FETCH NEXT FROM main_curs INTO @value;
CLOSE main_curs
DEALLOCATE main_curs

But it seems that it doesn’t pick up me setting the @someKey.

Any help on this would be greatly appreciated. Thanks!

UPDATE

I should include more information as I made the example seem too simple. I have multiple @someKey values that I need to use. As mentioned before, I have a parent-child relationship and I can have up to 6 children. So I am getting a list of parents and it’s respective columns and iterating through it. While in the WHILE-LOOP, I wanted to get the primary key from the parent and call another cursor to get the child information (different columns returned). So I would do multiple calls to the child cursor with different @someKey values set. Hope that makes sense.

  • 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-22T21:11:06+00:00Added an answer on May 22, 2026 at 9:11 pm

    One thing you could try is using nested cursors. An example of this is on the bottom of the page titled: Using nested cursors to produce report output.

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

Sidebar

Related Questions

Using parent child relationship where a parent can have children while each child has
I have a large result set assembled in a parent/child relationship. I need to
I have a parent-child relationship in an Oracle 9i database-table like: parent | child
I have a parent/child database relationship between the table ACCOUNT and the table USER.
I have the following table relationship in my database: Parent / \ Child1 Child2
I have parent/child relationship set up via Node Reference. A Child record can have
I have a existing parent-child relationship I am trying to map in Fluent Nhibernate:
I have db table with parent child relationship as: NodeId NodeName ParentId ------------------------------ 1
I have a LINQ-to-SQL DataContext that represents a parent-child relationship of Products to Prices.
I have a table which defines a child-parent relationship between nodes: CREATE TABLE node

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.