July 18, 2013 at 9:21 am
Hi,
I am looking for a script that will give all Heaps (user tables only ) in a sql server 2008 database. I have the following script but getting all of the tables/objects. Thanks.
use AdventureWorks
GO
SELECT SCHEMA_NAME(o.schema_id) AS [Schema]
,object_name(A1.object_id ) AS [Table Name] , B1.rows ,user_seeks ,user_scans
,user_lookups ,user_updates ,last_user_seek ,last_user_scan ,last_user_lookup
FROM sys.indexes A1
INNER JOIN sys.objects o ON A1.object_id = o.object_id
INNER JOIN sys.partitions B1 ON A1.object_id = B1.object_id AND A1.index_id =B1.index_id
LEFT OUTER JOIN sys.dm_db_index_usage_stats ius ON A1.object_id =ius.object_id AND A1.index_id = ius.index_id
WHERE A1.type_desc = 'HEAP'
ORDER BY rows desc
July 18, 2013 at 9:31 am
The script you posted should be returning only heaps. If you want to only view user tables you just need to add
and o.type_desc = 'USER_TABLE'
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply