Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
SQL

Get a Tree view with SQL Query

- 27.05.18 | 07.06.22 - ErcanOPAK

First of all, I must thank to Maulik Dhorajia for his great post. That post saved me a lot.

That’s the @Company table we will use:

And here is the SQL Query we need to use:

-- Working Example
;WITH CTECompany
AS
(
    SELECT 
        EmpID, 
        ParentID, 
        PersonName , 
        0 AS HLevel,
        CAST(RIGHT(REPLICATE('_',5) +  CONVERT(VARCHAR(20),EmpID),20) AS VARCHAR(MAX)) AS OrderByField
    FROM @Company
    WHERE ParentID IS NULL
    
    UNION ALL
    
    SELECT 
        C.EmpID, 
        C.ParentID, 
        C.PersonName , 
        (CTE.HLevel + 1) AS HLevel,
        CTE.OrderByField + CAST(RIGHT(REPLICATE('_',5) +  CONVERT(VARCHAR(20),C.EmpID),20) AS VARCHAR(MAX)) AS OrderByField
    FROM @Company C
    INNER JOIN CTECompany CTE ON CTE.EmpID = C.ParentID
    WHERE C.ParentID IS NOT NULL
)

-- Working Example
SELECT 
    EmpID
    , ParentID
    , HLevel
    , PersonName
    , (REPLICATE( '----' , HLevel ) + PersonName) AS Person
FROM CTECompany
ORDER BY OrderByField,PersonName;

Get a Tree view with SQL Query

Related posts:

SQL 'string_split()' function
sp_help for SQL Server Management Studio
How to solve 'Size property has an invalid size of 0' in C#
DATEADD Function in SQL
How to return only the Date from a SQL Server DateTime datatype
Post Views: 155

Post navigation

Database stuck in “Restoring” state
Fire Combobox SelectedIndexChanged with button code-behind

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

August 2022
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031  
« Jul    

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (362)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (234)
  • Find numbers with more than two decimal places in SQL (203)
  • Confirm before process with ASPxButton in Devexpress (200)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (199)
  • Add Constraint to SQL Table to ensure email contains @ (199)
  • How to solve “Response.Redirect cannot be called in a Page callback” for DevExpress Components (197)
  • How to make some specific word(s) Bold or Underline in ReportViewer (191)
  • Devexpress ASPxGridview Column Grouping in Code (183)
  • Tense Changes When Using Reported Speech (171)

Recent Posts

  • How to create the ShowBlanksValue and ShowNonBlanksValue items in Devex Grid
  • How to get ‘n’th row in Sql Query with OFFSET FETCH NEXT and ROW_NUMBER()
  • How to change the style of scrollbar with CSS
  • How to add scroll bar to div in Asp.Net
  • How to solve “Fatal: Not possible to fast-forward, aborting” problem
  • How to add Output Parameter to SqlDataSource
  • How to get stored procedure parameters details in SQL
  • How to add default value for Entity Framework migrations for DateTime and Bool
  • String Interpolation, String Format, String Concat and String Builder in C#
  • How to get triggers create and update date in SQL

Recent Posts

  • How to create the ShowBlanksValue and ShowNonBlanksValue items in Devex Grid
  • How to get ‘n’th row in Sql Query with OFFSET FETCH NEXT and ROW_NUMBER()
  • How to change the style of scrollbar with CSS
  • How to add scroll bar to div in Asp.Net
  • How to solve “Fatal: Not possible to fast-forward, aborting” problem

Most Viewed Posts

  • Get the First and Last Word from a String or Sentence in SQL (362)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (234)
  • Find numbers with more than two decimal places in SQL (203)
  • Confirm before process with ASPxButton in Devexpress (200)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (199)

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2022 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com