views:

12

answers:

1

In MS SQL 2005, is it possible to find out which tables/columns are being used either as keys in another table, or as part of a stored procedure?

The reason is I'm trying to clean up some old stored procs and tables, some of which can be removed, some of which can have columns pruned. But obviously I don't want to remove stuff which is being used.

A: 
DECLARE @BaseObject varchar(100)
SET @BaseObject = 'AddEntry'

SET NOCOUNT ON

DECLARE @Objects TABLE (
    id int
)

INSERT @Objects (id)
SELECT id FROM sysobjects
WHERE name like @BaseObject

WHILE (@@ROWCOUNT > 0)
BEGIN
    INSERT @Objects (id)
    SELECT d.depid
    FROM sysdepends d
    WHERE d.id IN (SELECT id FROM @Objects)
    AND d.depid NOT IN (SELECT id FROM @Objects)
END

SET NOCOUNT OFF

SELECT convert(varchar(100),
        '[' + oo.name + '].[' + o.name + ']') AS '--Object--'
FROM sysobjects o 
INNER JOIN sysusers oo ON o.uid = oo.uid
WHERE o.id IN (SELECT id FROM @Objects)
ORDER BY oo.name, o.name
Jonathan
Just tried it, very useful.
Carlos