Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

SQLServer - Wie finde ich abhängige Tabellen in meiner Tabelle?

Der Weg zu NUR TABELLE Referenzen (d. h. Tabellen, die eine bestimmte Tabelle als Fremdschlüssel verwenden, und Tabellen, die eine bestimmte Tabelle auf die gleiche Weise verwendet), können Sie dieses Code-Snippet verwenden:

declare @tableName varchar(64);
set @tableName = 'TABLE';

select
SO_P.name as [parent table]
,SC_P.name as [parent column]
,'is a foreign key of' as [direction]
,SO_R.name as [referenced table]
,SC_R.name as [referenced column]
,*
from sys.foreign_key_columns FKC
inner join sys.objects SO_P on SO_P.object_id = FKC.parent_object_id
inner join sys.columns SC_P on (SC_P.object_id = FKC.parent_object_id) AND (SC_P.column_id = FKC.parent_column_id)
inner join sys.objects SO_R on SO_R.object_id = FKC.referenced_object_id
inner join sys.columns SC_R on (SC_R.object_id = FKC.referenced_object_id) AND (SC_R.column_id = FKC.referenced_column_id)
where
    ((SO_P.name = @tableName) AND (SO_P.type = 'U'))
    OR
    ((SO_R.name = @tableName) AND (SO_R.type = 'U'))