How do I write a SQL Server query that gives me the table names given a FK name? -
this question has answer here:
i have fk: "fk_blahblah"
i want know 2 tables related it.
how can write query this?
this query returns info you're need... can add clause filter specific fk names, etc.
select t.name tablename, (select cc.name sys.columns cc, sys.tables tt tt.object_id = cc.object_id , tt.object_id = fk.parent_object_id , cc.column_id = fc.parent_column_id) column_name, fc.constraint_column_id, fk.name fkname, objectproperty(fk.object_id, 'cnstisdisabled') is_disabled, objectproperty(fk.object_id, 'cnstisnottrusted') is_untrusted, object_name(fk.referenced_object_id) referenced_table_name, (select cc.name sys.columns cc, sys.tables tt tt.object_id = cc.object_id , tt.object_id = fk.referenced_object_id , cc.column_id = fc.referenced_column_id) referenced_column_name, fk.update_referential_action, fk.delete_referential_action sys.foreign_keys fk inner join sys.tables t on fk.parent_object_id = t.object_id inner join sys.columns c on t.object_id = c.object_id inner join sys.foreign_key_columns fc on c.column_id = fc.parent_column_id , fc.constraint_object_id = fk.object_id , fc.parent_object_id = fk.parent_object_id , c.column_id = fc.parent_column_id order t.name, fk.name, fc.constraint_column_id
Comments
Post a Comment