How to get result from parent child table

前端 未结 4 1119
一个人的身影
一个人的身影 2021-01-22 14:44

Work on SQL-Server. My table structure is below

CREATE TABLE [dbo].[AgentInfo](
    [AgentID] [int] NOT NULL,
    [ParentID] [int] NULL,
 CONST         


        
4条回答
  •  野性不改
    2021-01-22 15:03

    You could use a recursive SELECT, see the examples in the documentation for WITH, starting with example D.

    The general idea within the recursive WITH is: You have a first select that is the starting point, and then a UNION ALL and a second SELECT which describes the step from on level to the next, where the previous level can either be the result of the first select or the result of the previous run of the second SELECT.

提交回复
热议问题