SQL Server 2005 recursive query with loops in data - is it possible?

前端 未结 10 1848
野趣味
野趣味 2020-12-06 03:29

I\'ve got a standard boss/subordinate employee table. I need to select a boss (specified by ID) and all his subordinates (and their subrodinates, etc). Unfortunately the rea

10条回答
  •  醉话见心
    2020-12-06 04:20

    The preferrable solution is to clean up the data and to make sure you do not have any loops in the future - that can be accomplished with a trigger or a UDF wrapped in a check constraint.

    However, you can use a multi statement UDF as I demonstrated here: Avoiding infinite loops. Part One

    You can add a NOT IN() clause in the join to filter out the cycles.

提交回复
热议问题