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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:28:48+00:00 2026-06-04T11:28:48+00:00

DECLARE @MAIN TABLE ( MainID INT NOT NULL, Name VARCHAR(20) NOT NULL, [Deleted] BIT

  • 0
DECLARE @MAIN TABLE
(
  MainID INT NOT NULL, 
  Name VARCHAR(20) NOT NULL,
  [Deleted] BIT NOT NULL
);

DECLARE @ITEMS TABLE
(
  ItemID INT NOT NULL, 
  ParentItemID INT NULL,
  MainID INT NOT NULL, 
  Data VARCHAR(100) NOT NULL
);


INSERT INTO @MAIN (MainID, Name, [Deleted]) VALUES  (1, 'Tool 1', 0)
INSERT INTO @MAIN (MainID, Name, [Deleted]) VALUES  (2, 'Tool 2', 0)

INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (1, NULL, 1, 'Level 1')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (2, NULL, 1, 'Level 2')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (3, NULL, 2, 'Level 1 - Irrelevant')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (4, NULL, 1, 'Level 3')

INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (5, 1, 1, 'Item 1-A')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (6, 4, 1, 'Item 2-C')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (7, 7, 2, 'Item Irrelevant 1')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (8, 10, 1, 'Item 3-E')

INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (9, 1, 1, 'Item 1-B')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (10, 4, 1, 'Item 2-D')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (11, 7, 2, 'Item Irrelevant 2')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (12, 10, 1, 'Item 3-F')

INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (13, 5, 1, 'Item 1-A-1')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (14, 8, 1, 'Item 2-C-1')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (15, 5, 2, 'Item 1-A-2')
INSERT INTO @ITEMS (ItemID, ParentItemID, MainID, Data) VALUES (16, 9, 1, 'Item 1-B-1')


SELECT * FROM @MAIN
SELECT * FROM @ITEMS

QUESTION 1:
What’s the easiest way to retrieve all record that belong to ItemID = 1 and ALL it’s recursive Sub Items and order by ParentHood.

QUESTION 2:
What’s the easiest way to retrieve all records order by ParentHood.

Thanks a lot ..in advanced !!

  • 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-04T11:28:49+00:00Added an answer on June 4, 2026 at 11:28 am

    What you need is to use a common-table expression which lets you execute a hierarchical query.

    ;With AllItems As
        (
        Select I.ItemId, Null As ParentItemId, M.MainId, M.Name, M.[Deleted]
        From @Items As I
            Join @Main As M
                On M.MainID = I.MainID
        Where ParentItemId Is Null
        Union All
        Select I.ItemId, I.ParentItemId, M.MainId, M.Name, M.[Deleted]
        From @Items As I
            Join AllItems As P
                On P.ItemId = I.ParentItemId
            Join @Main As M
                On M.MainID = I.MainID
        )
    Select *
    From AllItems
    

    WITH common_table_expression (Transact-SQL)

    SQL Fiddle Example

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

Sidebar

Related Questions

DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION
I'm trying to declare a method in main.h like this: void buildGraph(int gNum, Graph**
Here's a simplified example. UserDetails (UserID INT UserGUID UNIQUEIDENTIFIER Name VARCHAR(50) Age INT) UserRatings
DECLARE @CURRENTSCHOOL TABLE (STUDENT VARCHAR(8), COURSE VARCHAR(8), SCHOOL VARCHAR(2)) INSERT INTO @CURRENTSCHOOL VALUES ('10000000','MCR1010','11')
declare @servername varchar(2000) set @EmriServerit=(select @@servername) declare @dbname varchar(2000) set @dbname ='Test1' declare @Dir
declare @d varchar set @d = 'No filter' if (@d like 'No filter') BEGIN
DECLARE @lastname VARCHAR(25) DECLARE @birthdate VARCHAR(25) SELECT @lastname = 'Smith' SELECT @birthdate = '19-Apr-36'
DECLARE @Symb varchar SET @Symb = 'Lara' SELECT Names FROM Cricket where Names like
declare c int set c = 1 while c<700 do update users set profile_display_name
String[] items = new String[10]; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //

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.