Categories
Latest
Popular

SQL Server Get all Tables related to a Table with foreign Key

 

In cases where we need to find tables related to a table so that all can be dropped for re creation for a table use the following to list tables which are linked

Image

 

SELECT
‘drop TABLE ‘ + OBJECT_NAME(parent_object_id)
FROM sys.foreign_keys
WHERE referenced_object_id = object_id(‘table1′)

Replace table1 with your table name, copy the result text and execute (drop)

NB: This a drop statement you can very well alter it with rest SQL Statements in place of ‘drop TABLE ‘ , eg. ‘Select * from ‘