Quantcast
Channel: Forum SQL Server Database Engine
Viewing all articles
Browse latest Browse all 15694

dm_db_index_physical_stats - i want to query for all databases but im only getting results for currently selected db

$
0
0

Using SQL 2005

Like the title says, I thought I could get a result that include ALL databases on the instance, not just the currently selected database.  At least thats that the docs seem to indicate.

http://msdn.microsoft.com/en-us/library/ms188917.aspx
Specify NULL to return information for all databases in the instance of SQL Server. If you specify NULL fordatabase_id, you must also specify NULL forobject_id, index_id, andpartition_number.

Here is my query.  Ive currently got the master db selected in SSMS, and thats the only DB i get results for, yet I have 4 or 5 other DB's on the instance.

Help?

SELECT DB_NAME(database_id) AS DBname, OBJECT_NAME(a.object_id) as ObjectName,  
a.index_id, b.name as IndexName, avg_fragmentation_in_percent, index_type_desc
FROM sys.dm_db_index_physical_stats (NULL, NULL, NULL, NULL, NULL) AS a
JOIN sys.indexes AS b  
ON a.object_id = b.object_id AND a.index_id = b.index_id
WHERE b.index_id <> 0 and avg_fragmentation_in_percent > 30





Viewing all articles
Browse latest Browse all 15694

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>