Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tsql script to find tables not being used by stored procedures, views, functions, etc?

Is there a t-sql script to find out tables that aren't being used in sql server by stored procudures, views, functions, etc. I have a database that has 100s of tables, if not more and before I go dropping tables, I wanted to know if there was a script that could go through each object in the database and tell me if any tables are in use.

like image 543
Xaisoft Avatar asked Sep 18 '09 16:09

Xaisoft


People also ask

How do you check if a table is being used in a stored procedure?

Using SQL query, we can find out the list of the tables used in the stored procedure, using types of joins like inner join, outer join etc. Using SYSOBJECTS and SYSDEPENDS, we can get all the tables, stored procedure and other database object-related information.

How do you check if a table is being used in SQL Server?

Look in sys. dm_db_index_usage_stats. The columns last_user_xxx will contain the last time the table was accessed from user requests. This table resets its tracking after a server restart, so you must leave it running for a while before relying on its data.


3 Answers

If you want using a script, here (Listing SQL Server Object Dependencies) is a very good article how to script dependencies. Using that, you can make a list of tables being referenced. You have the list of tables that are in your database, so you know which of them are not being used.

In the article they use

sp_depends
stored procedure. However it has one failure. For example, if you have a stored procedure that used table "MyTable" and you create the procedure before you create the table "MyTable" you won't see this on the list of dependencies. That's why you should search the table
syscomments
to find dependencies. But this is also not accurate, because if you have the name of the table in the comment, you will treat it as a dependency.
like image 175
Lukasz Lysik Avatar answered Oct 26 '22 23:10

Lukasz Lysik


As far as I know you cannot really rely on SQL Server's dependency management. Lukasz pointed out one of the many issues.

In 2005, the equivalent of the old syscomments table is sys.sql_modules.

To find all table names which do not occur in TSQL code (views, SPs, functions), use this statement:

select t.name, sys.objects.name foundin, sys.objects.type_desc
from sys.objects t 
left outer join 
    sys.sql_modules
    inner join sys.objects on sys.objects.object_id = sys.sql_modules.object_id
on sys.sql_modules.definition like '%' + t.name + '%'
where t.type = 'U'
and sys.objects.name is null
order by t.name, type_desc, foundin

If you comment out the line with the IS NULL condition, you find all occurrences of all table names in TSQL code. (no matter if the table name really refers to that table)

like image 37
devio Avatar answered Oct 27 '22 00:10

devio


If you're using management studio, you can right-click on a table and 'view dependencies.'

And here is a link to an article on how to do this in tsql, which I'm guessing is what you're looking for:

https://web.archive.org/web/1/http://blogs.techrepublic%2ecom%2ecom/datacenter/?p=277

You could always build up a temp table / var with all the tables that have dependencies, and compare it against all tables to see what doesn't have any dependencies.

like image 44
ScottE Avatar answered Oct 26 '22 23:10

ScottE