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 ‘

abhilash

abhilash, is a programmer by profession and a technology addict with a passion towards browsing , shopping and imaging . My views may be biased on reviews. all images and videos are copyrighted to respective owners.

Add comment

Your Header Sidebar area is currently empty. Hurry up and add some widgets.