Click here to Skip to main content
13,298,175 members (79,579 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 15 Jan 2012

Get Tables List With Its Dependent Tables Names

, 15 Jan 2012
Rate this:
Please Sign up or sign in to vote.
Get Tables List With Its Dependent Tables Names
This script is useful to find dependent table list in SQL Server 2005,2008. It is more useful when we have to prepare cleanup script for database which has large number of tables and lots of dependence on other tables.

We can identify then which table is dependent on which tables, so we can easlly write cleanup query and also we can easily identify master tables.

SQL Query as below:
-- Declare Variables
DECLARE @mastertable VARCHAR(100)
DECLARE @TableCompleteName VARCHAR(100)
DECLARE @tablesname VARCHAR(1000)
-- declare temp table which is used for storing table name and dependent table list
CREATE TABLE #temptable
      tablecompletename VARCHAR(100),
      tablename VARCHAR(1000)
--  get all table name from sys objects 
DECLARE tmp_cur CURSOR static
    FOR SELECT + '.' +,
        FROM    sys.objects o
                INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
        WHERE   type = 'U'
        ORDER BY,
 -- fetch all table name    
OPEN tmp_cur
-- get fetch one by one table name and it's dependent table name     
FETCH FIRST FROM tmp_cur INTO @TableCompleteName, @mastertable
        SELECT  @tablesname = COALESCE(@tablesname + ',', '') + + '.'
                + OBJECT_NAME(FKEYID)
                INNER JOIN sys.objects o ON o.object_id = SYSFOREIGNKEYS.fkeyid
                INNER JOIN sys.schemas s ON s.schema_id = o.schema_id
        WHERE   OBJECT_NAME(RKEYID) = @mastertable
        INSERT  INTO #temptable
                SELECT  @TableCompleteName,
                        COALESCE(@tablesname, '')
        SELECT  @tablesname = NULL
        FETCH NEXT FROM tmp_cur INTO @TableCompleteName, @mastertable
SELECT  tablecompletename AS TableName, tablename AS DependentTables
FROM    #temptable 
DROP TABLE #temptable
CLOSE tmp_cur


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dhol Gaurav
Technical Lead eInfochips Pvt Ltd
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralGood thought... Please refer 'alternate1' to avoid usage of... Pin
Sangunni15-Jan-12 20:02
memberSangunni15-Jan-12 20:02 
GeneralRe: Hello It's Good but it's give us dependency of SP ,View and... Pin
Dhol Gaurav16-Jan-12 7:20
memberDhol Gaurav16-Jan-12 7:20 
GeneralReason for my vote of 5 Nice one! Not something I have to do... Pin
OriginalGriff14-Jan-12 23:18
mvpOriginalGriff14-Jan-12 23:18 
Reason for my vote of 5
Nice one!
Not something I have to do often, but usefull when I do.
GeneralRe: Thank You Pin
Dhol Gaurav15-Jan-12 7:48
memberDhol Gaurav15-Jan-12 7:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 15 Jan 2012
Article Copyright 2012 by Dhol Gaurav
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid