Monday, July 20, 2009

Find empty tables in SQL Server 2005

To find all the empty tables in your SQL Server 2005 you can use the following query:

SELECT o.name, sum(s.row_count)
FROM sys.objects o
JOIN sys.dm_db_partition_stats s
ON o.object_id=s.object_id
WHERE o.type='U'
GROUP BY o.name
HAVING sum(s.row_count) = 0
ORDER BY sum(s.row_count) desc

where 'U' stands for User Table

Just in case one wants to find out if a table is used in stored procedures, triggers and so on... one can use the following SQL:

SELECT o.name,o.xtype,m.definition
FROM sys.sql_modules m
INNER JOIN sysobjects o ON m.object_id=o.id
WHERE [definition] LIKE '%TABLE_NAME%'

No comments:

Post a Comment