Situatie
Mai jos este o procedura ce primeste ca si parametru un nume de tabela si pentru tabela respectiva listeaza relatiile pe care le are cu alte tabele, in cazul in care sunt definite FK-uri pe tabela respectiva.
Solutie
CREATE PROCEDURE FindTableRelationships
@table varchar(50)
as
Select
object_name(rkeyid) Parent_Table,
object_name(fkeyid) Child_Table,
object_name(constid) FKey_Name,
c2.name Parent_Table_Column,
c1.name Child_Table_Column
From
sys.sysforeignkeys s
Inner join sys.syscolumns c1
on ( s.fkeyid = c1.id And s.fkey = c1.colid )
Inner join syscolumns c2
on ( s.rkeyid = c2.id And s.rkey = c2.colid )
where object_name(rkeyid) = @table
or object_name(fkeyid) = @table
Order by Parent_Table,Child_Table
Leave A Comment?