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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:49:03+00:00 2026-05-14T04:49:03+00:00

A few weeks ago, I asked a question about how to generate hierarchical XML

  • 0

A few weeks ago, I asked a question about how to generate hierarchical XML from a table, that has a parentID column.
It all works fine. The point is, according to the hierarchy, I also want to query a table.

I’ll give you an example:

Thats the table with the codes:

ID          CODE         NAME                                               PARENTID
1           ROOT         IndustryCode                                       NULL
2           IND          Industry                                           1
3           CON          Consulting                                         1
4           FIN          Finance                                            1
5           PHARM        Pharmaceuticals                                    2
6           AUTO         Automotive                                         2
7           STRAT        Strategy                                           3
8           IMPL         Implementation                                     3
9           CFIN         Corporate Finance                                  4
10          CMRKT        Capital Markets                                    9

From which I generate (for displaying in a TreeViewControl) this XML:

<record key="1" parentkey="" Code="ROOT" Name="IndustryCode">
  <record key="2" parentkey="1" Code="IND" Name="Industry">
    <record key="5" parentkey="2" Code="PHARM" Name="Pharmaceuticals" /> 
    <record key="6" parentkey="2" Code="AUTO" Name="Automotive" /> 
  </record>
  <record key="3" parentkey="1" Code="CON" Name="Consulting">
    <record key="7" parentkey="3" Code="STRAT" Name="Strategy" /> 
    <record key="8" parentkey="3" Code="IMPL" Name="Implementation" /> 
  </record>
  <record key="4" parentkey="1" Code="FIN" Name="Finance">
    <record key="9" parentkey="4" Code="CFIN" Name="Corporate Finance">
      <record key="10" parentkey="9" Code="CMRKT" Name="Capital Markets" /> 
    </record>
  </record>
</record>

As you can see, some codes are subordinate to others, for example AUTO << IND << ROOT

What I want (and have absolutely no idea how to realise or even, where to start) is to be able to query another table (where one column is this certain code of course) for a code and get all records with the specific code and all subordinate codes

For example: I query the other table for “IndustryCode = IND[ustry]” and get (of course) the records containing “IND”, but also AUTO[motive] and PHARM[aceutical] (= all subordinates)

Its an SQL Express Server 2008 with Advanced Services.

  • 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-14T04:49:03+00:00Added an answer on May 14, 2026 at 4:49 am

    The way I usually do this is by adding a keychain column. For your data:

    ID          PARENTID       KEYCHAIN
    1           NULL           1
    2           1              1.2
    3           1              1.3
    4           1              1.4
    5           2              1.2.5
    6           2              1.2.6
    7           3              1.3.7
    8           3              1.3.8
    9           4              1.4.9
    10          9              1.4.9.10
    

    This column would obviously have to be calculated at insert using the generated id, but once it’s in there you can write your query quite simply.

    SELECT *
    FROM mytable
    WHERE KEYCHAIN like '1.2.%' or KEYCHAIN = '1.2'
    

    There are probably other ways to do this, but I’ve found this method to work pretty well.

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

Sidebar

Related Questions

A few weeks ago I asked a question about eliminating duplicate records in a
I asked from few weeks ago this question: How can I teach a beginner
A few weeks ago I asked about Application Servers. It happens that my bosses
A few weeks ago i asked a question about a multilanguage site (see Multilanguage
A few weeks ago I asked the question Is a PHP, Python, PostgreSQL design
I used a query a few weeks ago in MySQL that described a table
I asked about this on the jquery forum a few weeks ago without luck,
A few weeks ago I asked exactly the same question here . At first,
Few weeks ago I asked a question on increasing the speed of a function
i asked this a few weeks ago, but couldnt get any of the suggested

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.