views:

82

answers:

2

Is there a tool that will find all objects in SQL Server (functions, procs, views) that cannot possibly work because they refer to objects that don't exist?

+3  A: 

You may be interested in checking out the following articles:

You can test Michael J. Swart's solution as follows:

CREATE PROCEDURE proc_bad AS
    SELECT col FROM nonexisting_table
GO

SELECT
    OBJECT_NAME(referencing_id) AS [this sproc or VIEW...],
    referenced_entity_name AS [... depends ON this missing entity name]
FROM 
    sys.sql_expression_dependencies
WHERE 
    is_ambiguous = 0
    AND OBJECT_ID(referenced_entity_name) IS NULL
ORDER BY 
    OBJECT_NAME(referencing_id), referenced_entity_name;

Which returns:

+------------------------+------------------------------------------+
| this sproc or VIEW...  |  ... depends ON this missing entity name |
|------------------------+------------------------------------------|
| proc_bad               |  nonexisting_table                       |
+------------------------+------------------------------------------+
Daniel Vassallo
That looks really promising. There is a slight flaw in the where clause though. It needs " AND ISNULL(sys.sql_expression_dependencies.referenced_database_name, 'XXX') = 'XXX'" if you use cross-database queries.
Jonathan Allen
I endorse this solution :-) Keep in mind it relies on sys.sql_expression_dependencies which is a new view in SQL Server 2008.
Michael J Swart
A: 

Your best bet is to start using a tool like Visual Studio Database Edition. It's role is to manage a database schema. One of the many things it will do is to throw an error when you attempt to build the database project and it contains broken objects. It will of course do much more than this. The tool is free to any user of Visual Studio Team Suite or Visual Studio Developer Edition.

Randy Minder
Alas that won't work. That product doesn't handle circular references between databases.
Jonathan Allen