SQL Server: Show to show Parent Child Data

Sudip Bhatt 2,276 Reputation points
2021-02-04T11:45:33.89+00:00

I have a table where parent child relation maintain by ID and ParentID.

Now how could i display the Parent Child Data hierarchy. where Parent will come then its child will be shown.
Top Most Parent has 0 value in ParentID field.

Please discuss with a example

Thanks

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,665 questions
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 45,366 Reputation points
    2021-02-04T12:24:32.8+00:00

    Hello,

    For this you can use a recursive CTE (common table expression), see
    WITH common_table_expression (Transact-SQL)
    => Guidelines for Defining and Using Recursive Common Table Expressions
    => Example D. Using a recursive common table expression to display multiple levels of recursion

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.