This code involves a recursive Stored Procedure call and a \"not so great\" method of avoiding cursor name collision. In the end I don\'t care if it uses cursors or not. Just lo
See this Stackoverflow question for a discussion of sorting querying table foreign key dependencies by depth - which is a similar problem to the one you're discussing. There are at least two working solutions to that problem in the answers and the only real difference to what you're doing is the tables they're crawling. This posting has a DB reverse engineering script that shows how to use a lot of the main data dictionary tables.