September 14, 2015 at 11:14 am
Select A.name, B.name
FROM
SYS.tables A
inner join
SYS.columns B on ( A.object_id = B.object_id )
ORDER BY 1 , 2
Can anyone help me so that i can add a 3 rd column to the SQL statement
where it shows if the column is a foriegn key and then where is it pointing to ?
Great and Thanks ahead
September 14, 2015 at 11:31 am
What have you tried so far?
-- Itzik Ben-Gan 2001
September 14, 2015 at 11:37 am
Will this work for you?
SELECT so.name table_name, sc.name column_name, fk.name fk_name, so_fk.name references_table, sc_fk.name referenced_column
FROM sys.objects so
INNER JOIN sys.columns sc on sc.object_id = so.object_id
LEFT OUTER JOIN sys.foreign_key_columns fkc ON fkc.parent_object_id = sc.object_id
AND fkc.parent_column_id = sc.column_id
LEFT OUTER JOIN sys.foreign_keys fk ON fkc.constraint_object_id = fk.object_id
LEFT OUTER JOIN sys.objects so_fk ON fkc.referenced_object_id = so_fk.object_id
LEFT OUTER JOIN sys.columns sc_fk ON fkc.referenced_column_id = sc_fk.column_id
AND fkc.referenced_object_id = sc_fk.object_id
WHERE so.type = 'U'
ORDER BY so.name, sc.name;
September 14, 2015 at 1:58 pm
Ed Wagner (9/14/2015)
Will this work for you?
SELECT so.name table_name, sc.name column_name, fk.name fk_name, so_fk.name references_table, sc_fk.name referenced_column
FROM sys.objects so
INNER JOIN sys.columns sc on sc.object_id = so.object_id
LEFT OUTER JOIN sys.foreign_key_columns fkc ON fkc.parent_object_id = sc.object_id
AND fkc.parent_column_id = sc.column_id
LEFT OUTER JOIN sys.foreign_keys fk ON fkc.constraint_object_id = fk.object_id
LEFT OUTER JOIN sys.objects so_fk ON fkc.referenced_object_id = so_fk.object_id
LEFT OUTER JOIN sys.columns sc_fk ON fkc.referenced_column_id = sc_fk.column_id
AND fkc.referenced_object_id = sc_fk.object_id
WHERE so.type = 'U'
ORDER BY so.name, sc.name;
I'd use sys.tables instead of sys.objects as that restricts me to the user tables from the start.
September 14, 2015 at 2:01 pm
Lynn Pettis (9/14/2015)
Ed Wagner (9/14/2015)
Will this work for you?
SELECT so.name table_name, sc.name column_name, fk.name fk_name, so_fk.name references_table, sc_fk.name referenced_column
FROM sys.objects so
INNER JOIN sys.columns sc on sc.object_id = so.object_id
LEFT OUTER JOIN sys.foreign_key_columns fkc ON fkc.parent_object_id = sc.object_id
AND fkc.parent_column_id = sc.column_id
LEFT OUTER JOIN sys.foreign_keys fk ON fkc.constraint_object_id = fk.object_id
LEFT OUTER JOIN sys.objects so_fk ON fkc.referenced_object_id = so_fk.object_id
LEFT OUTER JOIN sys.columns sc_fk ON fkc.referenced_column_id = sc_fk.column_id
AND fkc.referenced_object_id = sc_fk.object_id
WHERE so.type = 'U'
ORDER BY so.name, sc.name;
I'd use sys.tables instead of sys.objects as that restricts me to the user tables from the start.
Very true. I'm just so accustomed to using sys.objects for everything that I think I have a default set somewhere. 😛
September 14, 2015 at 2:03 pm
Ed Wagner (9/14/2015)
Lynn Pettis (9/14/2015)
Ed Wagner (9/14/2015)
Will this work for you?
SELECT so.name table_name, sc.name column_name, fk.name fk_name, so_fk.name references_table, sc_fk.name referenced_column
FROM sys.objects so
INNER JOIN sys.columns sc on sc.object_id = so.object_id
LEFT OUTER JOIN sys.foreign_key_columns fkc ON fkc.parent_object_id = sc.object_id
AND fkc.parent_column_id = sc.column_id
LEFT OUTER JOIN sys.foreign_keys fk ON fkc.constraint_object_id = fk.object_id
LEFT OUTER JOIN sys.objects so_fk ON fkc.referenced_object_id = so_fk.object_id
LEFT OUTER JOIN sys.columns sc_fk ON fkc.referenced_column_id = sc_fk.column_id
AND fkc.referenced_object_id = sc_fk.object_id
WHERE so.type = 'U'
ORDER BY so.name, sc.name;
I'd use sys.tables instead of sys.objects as that restricts me to the user tables from the start.
Very true. I'm just so accustomed to using sys.objects for everything that I think I have a default set somewhere. 😛
Also, I would use sys.foreign_key_columns in the query as well instead of linking back to sys.objects.
September 14, 2015 at 9:17 pm
Cool! Thanks, worked well
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply