mercoledì 20 agosto 2014

Query per trovare tabelle che hanno una FK verso la tabella in esame

Query per trovare tutte le foreign key verso una tabella, testato con Sql Server 2008

select t.name , fk.constraint_column_id  , c.name
from sys.foreign_key_columns as fk
inner join sys.tables as t on fk.parent_object_id = t.object_id
inner join sys.columns as c on fk.parent_object_id = c.object_id and fk.parent_column_id = c.column_id
where fk.referenced_object_id = (select object_id from sys.tables where name = 'NOME_TABELLA')
order by t.name , fk.constraint_column_id

Nessun commento:

Posta un commento