Nice Code.
I did the same thing with this query...
select ccu.table_schema + '.' + ccu.table_name as MTablename, ccu.column_name as Mcolname,
ccu1.table_schema + '.' + ccu1.table_name as Tablename, ccu1.column_name as colname
,ccu3.table_schema + '.' + ccu3.table_name as C2Tablename, ccu3.column_name as C2colname
,ccu5.table_schema + '.' + ccu5.table_name as C3Tablename, ccu5.column_name as C3colname
,ccu7.table_schema + '.' + ccu7.table_name as C4Tablename, ccu7.column_name as C4colname
,ccu9.table_schema + '.' + ccu9.table_name as C5Tablename, ccu9.column_name as C5colname
,ccu11.table_schema + '.' + ccu11.table_name as C6Tablename, ccu11.column_name as C6colname
from information_schema.constraint_column_usage CCU
inner join information_schema.referential_constraints RC on CCU.constraint_name=RC.unique_constraint_name
inner join information_schema.constraint_column_usage CCU1 on RC.constraint_name=ccu1.constraint_name
Left Outer join information_schema.constraint_column_usage CCU2 on CCU2.Table_Schema + '.' + CCU2.table_name = CCU1.Table_Schema + '.' + CCU1.table_name
Left Outer Join information_schema.referential_constraints RC2 on CCU2.constraint_name=RC2.unique_constraint_name
Left Outer Join information_schema.constraint_column_usage CCU3 on RC2.constraint_name=ccu3.constraint_name
Left Outer join information_schema.constraint_column_usage CCU4 on CCU4.Table_Schema + '.' + CCU4.table_name = CCU3.Table_Schema + '.' + CCU3.table_name
Left Outer Join information_schema.referential_constraints RC3 on CCU4.constraint_name=RC3.unique_constraint_name
Left Outer Join information_schema.constraint_column_usage CCU5 on RC3.constraint_name=ccu5.constraint_name
Left Outer join information_schema.constraint_column_usage CCU6 on CCU6.Table_Schema + '.' + CCU6.table_name = CCU5.Table_Schema + '.' + CCU5.table_name
Left Outer Join information_schema.referential_constraints RC4 on CCU6.constraint_name=RC4.unique_constraint_name
Left Outer Join information_schema.constraint_column_usage CCU7 on RC4.constraint_name=ccu7.constraint_name
Left Outer join information_schema.constraint_column_usage CCU8 on CCU8.Table_Schema + '.' + CCU8.table_name = CCU7.Table_Schema + '.' + CCU7.table_name
Left Outer Join information_schema.referential_constraints RC5 on CCU8.constraint_name=RC5.unique_constraint_name
Left Outer Join information_schema.constraint_column_usage CCU9 on RC5.constraint_name=ccu9.constraint_name
Left Outer join information_schema.constraint_column_usage CCU10 on CCU10.Table_Schema + '.' + CCU10.table_name = CCU9.Table_Schema + '.' + CCU9.table_name
Left Outer Join information_schema.referential_constraints RC6 on CCU10.constraint_name=RC6.unique_constraint_name
Left Outer Join information_schema.constraint_column_usage CCU11 on RC6.constraint_name=ccu11.constraint_name
where ccu.constraint_name not in (select constraint_name from information_schema.referential_constraints)
and ccu.table_schema + '.' + ccu.table_name in ('dbo.Portal_users')
The thing which makes the difference is that I have to add the joins manually if I have to add the level to which i need to go to find the childs. Your code is generic in this case.
I was just thinking to go into string processing to resolve this issue in my code, but thanks to you, now I will be using your code for my future developments.
Thanks once again and NICE CODE...
Atif Sheikh