Get hierarchical structure using SQL Server

后端 未结 4 551
广开言路
广开言路 2021-01-06 09:26

I have a self-referencing table with a primary key, id and a foreign key parent_id.

+------------+--------------+------+-----+-----         


        
4条回答
  •  醉梦人生
    2021-01-06 09:34

    i know changing the structure of a table is always a critical operation but since sql server 2008 introduced the HierarchyId Datatype i really like workig with it. Maybe have a look at:

    http://www.codeproject.com/Articles/37171/HierarchyID-Data-Type-in-SQL-Server http://www.codeproject.com/Tips/740553/Hierarchy-ID-in-SQL-Server

    I am sure you will understand quickly how to use this datatype and his functions. The SQL Code using this datatype is more structured and has better performance than CTE's.

提交回复
热议问题