SQL Server: Howto get foreign key reference from information_schema?

Never mind, this is the correct answer: msdn.microsoft.com/en-us/library/aa17580...).aspx select kcu1. Constraint_name as 'fk_constraint_name' , kcu1. Table_name as 'fk_table_name' , kcu1.

Column_name as 'fk_column_name' , kcu1. Ordinal_position as 'fk_ordinal_position' , kcu2. Constraint_name as 'referenced_constraint_name' , kcu2.

Table_name as 'referenced_table_name' , kcu2. Column_name as 'referenced_column_name' , kcu2. Ordinal_position as 'referenced_ordinal_position' from information_schema.

Referential_constraints rc join information_schema. Key_column_usage kcu1 on kcu1. Constraint_catalog = rc.

Constraint_catalog and kcu1. Constraint_schema = rc. Constraint_schema and kcu1.

Constraint_name = rc. Constraint_name join information_schema. Key_column_usage kcu2 on kcu2.

Constraint_catalog = rc. Unique_constraint_catalog and kcu2. Constraint_schema = rc.

Unique_constraint_schema and kcu2. Constraint_name = rc. Unique_constraint_name and kcu2.

Ordinal_position = kcu1. Ordinal_position.

If you can live with using the SQL Server specific schema catalog views, this query will return what you're looking for: SELECT fk. Name, OBJECT_NAME(fk. Parent_object_id) 'Parent table', c1.Name 'Parent column', OBJECT_NAME(fk.

Referenced_object_id) 'Referenced table', c2. Name 'Referenced column' FROM sys. Foreign_keys fk INNER JOIN sys.

Foreign_key_columns fkc ON fkc. Constraint_object_id = fk. Object_id INNER JOIN sys.

Columns c1 ON fkc. Parent_column_id = c1. Column_id AND fkc.

Parent_object_id = c1. Object_id INNER JOIN sys. Columns c2 ON fkc.

Referenced_column_id = c2. Column_id AND fkc. Referenced_object_id = c2.

Object_id Not sure how - if at all - you can get the same information from the INFORMATION_SCHEMA views....

I'm not sure, but I don't think it's correct. I get 349, which I think is more likely... – Quandary Oct 11 '10 at 16:17 @Quandary: sorry, missed a few ON conditions - this should be better now (works for me, anyway) – marc_s Oct 11 '10 at 16:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions