Invalid objects in SQL Server are database objects that reference something that no longer exists or has changed in a way that breaks the reference. The fastest way to find them is to check for objects that won't compile: run sp_refreshsqlmodule or sp_recompile against dependent objects, or query the system catalog views (sys.sql_expression_dependencies, sys.objects, sys.sql_modules) to surface objects whose referenced targets are missing. The key condition is that SQL Server doesn't validate dependencies at creation time by default, so an invalid object can sit dormant until someone executes it.

That's the core problem. You can rename a table, drop a column, or delete a function, and the views, stored procedures, and triggers that reference it will keep sitting there, looking perfectly healthy in Object Explorer. They only fail when called. In a production environment, that means an invalid object can go undetected for weeks until a user hits the exact code path that touches it, and then you're debugging under pressure.

This guide walks through the practical methods for finding invalid objects before they find you, from quick catalog queries to schema-wide recompilation checks. It covers SQL Server 2016 through 2022 and Azure SQL Database, with notes where behavior differs.

Why SQL Server lets invalid objects exist in the first place

SQL Server uses deferred name resolution for stored procedures and several other object types. When you create a procedure that references a table, SQL Server doesn't verify that the table exists at creation time. The reference is resolved when the procedure first executes. This design exists partly to allow creation order flexibility: you can create a procedure before the table it uses, or create objects in any sequence across a deployment script.

Views behave differently. CREATE VIEW does check that referenced objects exist at creation time, but it only checks at that moment. If you later drop a column from an underlying table, the view stays in place. SQL Server marks it as potentially invalid but doesn't drop or disable it. The view only fails when queried, and the error message usually points at the missing column.

There's a related concept called schema binding. When you create a view or function WITH SCHEMABINDING, SQL Server prevents changes to the underlying objects that would break the dependency. Schema-bound objects can't become invalid through column drops or table renames, because those operations are blocked. Non-schema-bound objects have no such protection, which is why they're the usual suspects when you go hunting.

The practical takeaway: invalid objects are normal in a healthy database, but undetected invalid objects are a risk. The goal isn't to eliminate the possibility but to find the objects that are actually broken right now.

Prerequisites and what you need before you start

Before running any of the checks below, confirm a few things.

You need at least VIEW DEFINITION permission on the objects you're inspecting, and EXECUTE permission if you plan to use sp_refreshsqlmodule. Many of the catalog views filter their output based on the caller's permissions, so a low-privilege login may see an incomplete picture. If you're doing a full database audit, run the queries as a member of db_owner or with sysadmin rights on a development copy first.

Work on a non-production copy when possible. The recompilation-based methods force SQL Server to re-parse and re-bind object definitions, which is generally safe but can briefly block schema changes or cause plan cache churn. On a busy production system, that's usually acceptable during a maintenance window, but it's not something to run casually at peak load.

Also note the scope. Some checks are database-scoped, meaning they only find invalid objects in the current database. Cross-database dependencies (a view in database A referencing a table in database B) won't show up unless you check each database separately, and even then the dependency metadata may not capture cross-database references reliably. Server-level objects like linked server references and some CLR objects need their own checks.

Method 1: Query the dependency catalog views

The most direct way to find broken references is to compare what objects claim to depend on against what actually exists. SQL Server exposes this through sys.sql_expression_dependencies, which records references between objects.

The query below finds objects whose referenced entities don't exist in the current database:

The logic here matters. When SQL Server can resolve a dependency to an object in the current database, it stores the referenced_id. When it can't resolve it (the object is missing, or it's a cross-database reference, or it's a deferred reference that was never bound), referenced_id is NULL. Filtering on referenced_database_name IS NULL excludes cross-database references, which would otherwise show up as false positives.

The is_ms_shipped = 0 filter drops system objects, which you don't want to touch. Without it, you'll get noise from Microsoft-shipped components.

What this query won't catch: it misses dependencies that were never recorded, and it can produce false positives for objects that use dynamic SQL. If a procedure builds a table name as a string and executes it with sp_executesql, there's no static dependency to check, and the object will look clean even if the string references a table that no longer exists. Dynamic SQL is invisible to this method, so treat a clean result as "no static broken references" rather than "no problems."

A variation worth running checks for dependencies where the referenced object exists but the specific column doesn't:

This catches the column-rename scenario, which is one of the most common causes of a view that suddenly fails.

Method 2: Use sp_refreshsqlmodule to force validation

Catalog queries tell you what SQL Server knows about dependencies. They don't force validation. To actually test whether an object compiles against the current schema, use sp_refreshsqlmodule. This procedure re-parses the object's definition and updates its metadata; if the definition references something missing, it raises an error.

The typical pattern is to loop through candidate objects and call the procedure inside a try/catch:

The object types in the filter cover views (V), stored procedures (P), scalar functions (FN), inline table-valued functions (IF), multi-statement table-valued functions (TF), and triggers (TR). Schema-bound objects are excluded because they can't be invalid in the usual sense; SQL Server blocks the changes that would break them.

A few caveats. sp_refreshsqlmodule on a procedure that uses dynamic SQL will succeed even if the dynamic SQL references missing objects, because the references aren't static. The procedure recompiles fine; it just fails later at runtime. It also won't catch cross-database references where the other database is offline or the referenced object is missing there.

Also, calling sp_refreshsqlmodule on a large number of objects generates schema-modification locks. On a busy OLTP database, that can cause brief blocking. Run it during a quiet period, or test the script against a restored copy first to see how long it takes and what it finds.

An alternative to sp_refreshsqlmodule is sp_recompile. It forces recompilation of a procedure or trigger on next execution, which is more intrusive and less useful for detection because it defers the parse. sp_refreshsqlmodule is the better tool here because it validates immediately.

Method 3: Recompile the whole database with sp_recompile

If you need a broad sweep without writing a cursor, you can force recompilation of every procedure in a database by running:

Wait, that's not quite right for modern SQL Server. sp_recompile accepts a table or procedure name, not a database name, and passing a database name isn't supported. The historical trick of marking the whole database for recompile on next access (using sp_recompile against a system table) is not reliable and isn't something you should depend on.

The practical alternative is the cursor approach above, or to use sp_refreshview for views specifically. sp_refreshview is a narrower version of sp_refreshsqlmodule that only handles views:

It's less commonly useful than the general procedure because you still need to iterate, but it exists and it's worth knowing if you're specifically hunting view problems.

A note on tooling: SQL Server Data Tools (SSDT) and the sqlpackage utility can validate a database project against a target database, and that's often a better way to catch invalid objects during development than runtime checks. The runtime methods in this guide are for finding problems in databases that are already deployed.

Method 4: Look for invalid objects across all databases

If you're auditing an instance rather than a single database, wrap the checks in a loop over sys.databases. The pattern looks like this:

The AT clause runs the query in the context of each database. This is a lightweight check and it's safe to run on production because it only reads metadata. Combine it with the cursor-based sp_refreshsqlmodule check if you want actual validation rather than just catalog inspection, but scale that one carefully.

Note that databases with state_desc other than ONLINE, including RESTORING and RECOVERING, are excluded because their metadata isn't consistently queryable. If you need to check a database that's currently in a transition state, wait for it to come online first.

What "invalid" actually means in different contexts

The word "invalid" gets used loosely, so it's worth separating the cases you'll encounter.

A broken reference is the most common meaning: the object references a table, view, or column that doesn't exist. This is what the catalog query and the recompilation check find. The object definition is syntactically fine; the target is gone.

A schema drift problem is subtler. The referenced object exists but its shape changed: a column was added with a different data type, a constraint was dropped, or a computed column changed. The object may still compile if the referenced names still resolve, but it can return wrong results or fail at runtime. Catalog dependency checks won't flag this, and sp_refreshsqlmodule will often succeed. Catching it requires comparing the actual definitions, which is a job for schema comparison tools rather than a single query.

An orphaned user or login is a different kind of invalid object: a database user whose corresponding server login was deleted. This doesn't break code, but it shows up in audits. The fix is sp_change_users_login or ALTER USER with a login mapping, not recompilation. The query to find them is straightforward:

A disabled or invalid agent job, a broken SSIS package step, or a linked server that can't connect are also sometimes called invalid objects, but they're outside the scope of what SQL Server's dependency metadata tracks. If you're doing a full health check, handle those separately through SQL Server Agent and the SSIS catalog.

Handling what you find: fix or drop

Once you have a list of invalid objects, the next question is what to do with each one. There are three realistic options.

Repair the reference. If the object references a table that was renamed or moved, update the definition with ALTER PROCEDURE, ALTER VIEW, or the appropriate statement. This is the right choice when the object is still needed and the reference can be restored. Use ALTER, not DROP and CREATE, because ALTER preserves permissions and other metadata.

Drop the object. If the object is genuinely obsolete (an old view nobody uses, a procedure replaced by a newer one), dropping it removes the problem and reduces clutter. Before dropping, check for dependents: sys.sql_expression_dependencies can tell you what references the object you're about to remove. Dropping a table that something depends on creates a new invalid object, so resolve the chain rather than the leaf.

Leave it and document it. Sometimes an object is invalid for a known reason (a reference to a cross-database table that's only mounted during month-end processing). In that case, record the condition so the next person who runs the check doesn't treat it as a new problem. A simple table of known-invalid objects, or a comment in the object definition, is enough.

Before any fix, capture the current definition. sys.sql_modules stores the full text, and dropping an object loses it permanently unless you've scripted it. A quick way to save definitions is:

Export the results before making changes, especially on production.

Check success and decide when to call in help

A successful cleanup has a verifiable end state. After fixing or dropping the objects you identified, re-run the catalog query and the sp_refreshsqlmodule loop. The catalog query should return zero rows for the objects you addressed, and the loop should complete without printing "INVALID" for them. If either still flags an object, the fix didn't take: check that you edited the right database, that the referenced object was restored in the right schema, and that permissions on the referencing object haven't changed.

A stop condition worth respecting: if the invalid objects are in system databases (master, msdb, model), or if they're Microsoft-shipped objects, don't attempt to drop or alter them. That's a signal of a deeper problem, such as a failed service pack or a corrupted system catalog, and the correct next step is to check SQL Server error logs, run DBCC CHECKDB on the affected database, and involve a qualified database administrator. Recompilation scripts aimed at user objects are routine maintenance; scripts aimed at system objects are how you turn a small problem into an unstartable instance.

For user objects, the remaining edge cases are dynamic SQL and cross-database references, and both require manual review rather than an automated check. If you've cleared the static checks and still have runtime failures, the next step is to search the object definitions for EXEC, sp_executesql, and three-part names, then trace each one by hand. That's slower, but it's the right tool for the part of the problem that metadata can't see.