• This is a really great use of data in the sys tables of your database, especially when it comes to providing documentation.

    One small bug in the procedure however is that it will duplicate parent child relationships in the resultant tree between two tables where the relationship is composed of a composite key. To rectify this you need to simply pouplate the cursor curChild with a distinct list of foreign keys, e.g.

    declare curChild cursor local for

            select distinct object_name(fkeyid) as child from sysforeignkeys

            where rkeyid = object_id(@table_name) and rkeyid <> fkeyid