Sql server 2008 正在从查询中的对象\ ID列出表名
我发现这个脚本将使用系统函数sys.dm_db_index_physical_stats分析数据库的碎片,并给出给定数据库的objectID、IndexID和%碎片,但我想知道表名和索引名,而不是ID 在此处找到脚本: 查询本身是:Sql server 2008 正在从查询中的对象\ ID列出表名,sql-server-2008,indexing,Sql Server 2008,Indexing,我发现这个脚本将使用系统函数sys.dm_db_index_physical_stats分析数据库的碎片,并给出给定数据库的objectID、IndexID和%碎片,但我想知道表名和索引名,而不是ID 在此处找到脚本: 查询本身是: SELECT object_id AS ObjectID, index_id AS IndexID, avg_fragmentation_in_percent AS PercentFragment, fragment_count AS TotalFrags, avg
SELECT
object_id AS ObjectID,
index_id AS IndexID,
avg_fragmentation_in_percent AS PercentFragment,
fragment_count AS TotalFrags,
avg_fragment_size_in_pages AS PagesPerFrag,
page_count AS NumPages
FROM
sys.dm_db_index_physical_stats(DB_ID('Hemasphere_Train'),NULL, NULL, NULL ,'DETAILED')
WHERE
avg_fragmentation_in_percent > 0
ORDER BY
ObjectID, IndexID
是否可以获取表名和索引名而不是ID?如果是这样,我应该选择什么呢?对于表格,使用函数
对于索引,请连接到sys.indexes
SELECT
ips.object_id AS ObjectID,
object_name(ips.object_id) as table_name,
ips.index_id AS IndexID,
i.name as index_name,
avg_fragmentation_in_percent AS PercentFragment,
fragment_count AS TotalFrags,
avg_fragment_size_in_pages AS PagesPerFrag,
page_count AS NumPages
FROM
sys.dm_db_index_physical_stats(DB_ID('Hemasphere_Train'),NULL, NULL, NULL ,'DETAILED') ips
inner join sys.indexes i
on ips.index_id = i.index_id
and ips.object_id = i.object_id
WHERE
avg_fragmentation_in_percent > 0
ORDER BY
ObjectID, IndexID
当我这样做时,表名和索引名都返回为“NULL”。@MISNole您是否在
Hemasphere\u Train
数据库中运行此查询?否,来自master。当我试图从Hemasphere\u Train
数据库运行它时,它抛出错误:行sys.dm\u db\u index\u physical\u stats(db\u ID('Hemasphere\u Train')、NULL、NULL、NULL、'DETAILED')上的“Hemasphere\u Train”附近语法不正确。