@@ -278,6 +278,8 @@ BEGIN TRY
278278 /* ******************/
279279 /* Get index stats */
280280 /* ******************/
281+ -- Use DBCC to avoid various inconsistencies
282+ -- in equivalent DMVs between 2012-2016
281283 SET @Sql = CONCAT(@UseDatabase, ' DBCC SHOW_STATISTICS ("' , @QualifiedTable,' ", ' , QUOTENAME(@IndexName), ' )' );
282284 EXEC sp_executesql @Sql;
283285
@@ -365,18 +367,17 @@ BEGIN TRY
365367 /* *************************/
366368 -- Row count
367369 SET @Sql = CONCAT(@UseDatabase,
368- N' SELECT @NumRows = SUM([ps].[row_count])
369- FROM [sys].[objects] AS [o]
370- INNER JOIN [sys].[dm_db_partition_stats] AS [ps] ON [o].[object_id] = [ps].[object_id]
371- WHERE [o].[type] = ' ' U' '
372- AND [o].[is_ms_shipped] = 0
373- AND [ps].[index_id] < 2
374- AND [o].[object_id] = @ObjectID
375- GROUP BY [o].[schema_id], [o].[name];' );
376- SET @ParmDefinition = N' @ObjectID BIGINT, @NumRows BIGINT OUTPUT' ;
370+ N' SELECT @NumRows = [sp].[rows] -- Accounts for index filter if in use
371+ FROM [sys].[objects] AS [o]
372+ INNER JOIN [sys].[stats] AS [stat] ON [stat].[object_id] = [o].[object_id]
373+ CROSS APPLY [sys].[dm_db_stats_properties]([stat].[object_id], [stat].[stats_id]) AS [sp]
374+ WHERE [o].[object_id] = @ObjectID
375+ AND [stat].[name] = @IndexName;' );
376+ SET @ParmDefinition = N' @ObjectID BIGINT, @IndexName SYSNAME, @NumRows BIGINT OUTPUT' ;
377377 EXEC sp_executesql @Sql
378378 ,@ParmDefinition
379379 ,@ObjectID
380+ ,@IndexName
380381 ,@NumRows OUTPUT;
381382
382383 IF (@Verbose = 1 )
0 commit comments