Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear All,

There is very simple way to write query for same.

SELECT
t.name table_name,
s.name SCHEMA_NAME,
sum(p.rows) total_rows
FROM
sys.tables t
join sys.schemas s on (t.schema_id = s.schema_id)
join sys.partitions p on (t.object_id = p.object_id)
WHERE p.index_id in (0,1)
GROUP BY t.name,s.name
HAVING sum(p.rows) = 0
Posted
Comments
Bhanu Pratap Verma 22-Jul-14 2:01am    
This is not a query, its already solved. It might be help to some one.
Maciej Los 22-Jul-14 2:02am    
Is it a question?

1 solution

This query is veryusefull not only for empty tables but also to get an overview of the space consumed totally and where:
SELECT 
    t.NAME AS TableName,
    s.Name AS SchemaName,
    p.rows AS RowCounts,
    SUM(a.total_pages) * 8 AS TotalSpaceKB, 
    SUM(a.used_pages) * 8 AS UsedSpaceKB, 
    (SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
WHERE 
    t.NAME NOT LIKE 'dt%' 
    AND t.is_ms_shipped = 0
    AND i.OBJECT_ID > 255 
	
GROUP BY 
    t.Name, s.Name, p.Rows
ORDER BY 
    t.Name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900