From 5c9314ef835bcb52145d94faf332802ceb5f069c Mon Sep 17 00:00:00 2001 From: jamiet Date: Mon, 30 Dec 2013 14:46:57 +0000 Subject: [PATCH] GetPartitionRowTallies --- TSQLCodeLibrary/TSQLCodeLibrary.sqlproj | 1 + .../GetPartitionRowTallies.sql | 17 ++++++++++++++ .../Stored Procedures/GetTableRowTallies.sql | 22 +++++++++---------- 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 TSQLCodeLibrary/jt/Stored Procedures/GetPartitionRowTallies.sql diff --git a/TSQLCodeLibrary/TSQLCodeLibrary.sqlproj b/TSQLCodeLibrary/TSQLCodeLibrary.sqlproj index 275599d..591ea73 100644 --- a/TSQLCodeLibrary/TSQLCodeLibrary.sqlproj +++ b/TSQLCodeLibrary/TSQLCodeLibrary.sqlproj @@ -80,6 +80,7 @@ + diff --git a/TSQLCodeLibrary/jt/Stored Procedures/GetPartitionRowTallies.sql b/TSQLCodeLibrary/jt/Stored Procedures/GetPartitionRowTallies.sql new file mode 100644 index 0000000..123c621 --- /dev/null +++ b/TSQLCodeLibrary/jt/Stored Procedures/GetPartitionRowTallies.sql @@ -0,0 +1,17 @@ +CREATE PROCEDURE jt.GetPartitionRowTallies + @dbName SYSNAME = 'master' +AS +BEGIN + DECLARE @sql NVARCHAR(MAX) = ' + SELECT t.name + ,s.partition_number + ,s.row_count + FROM [@dbname].sys.dm_db_partition_stats AS s + INNER JOIN [@dbname].sys.tables AS t ON t.[object_id] = s.[object_id] + GROUP BY t.name + ,s.partition_number + ,s.row_count; + '; + SET @sql = REPLACE(@sql,'@dbName',@dbName) + EXEC (@sql) +END \ No newline at end of file diff --git a/TSQLCodeLibrary/jt/Stored Procedures/GetTableRowTallies.sql b/TSQLCodeLibrary/jt/Stored Procedures/GetTableRowTallies.sql index 19bccee..27c0b8a 100644 --- a/TSQLCodeLibrary/jt/Stored Procedures/GetTableRowTallies.sql +++ b/TSQLCodeLibrary/jt/Stored Procedures/GetTableRowTallies.sql @@ -2,16 +2,14 @@ @dbName SYSNAME = 'master' AS BEGIN - DECLARE @sql NVARCHAR(MAX) = ' - SELECT t.name - ,s.partition_number - ,s.row_count - FROM [@dbname].sys.dm_db_partition_stats AS s - INNER JOIN [@dbname].sys.tables AS t ON t.[object_id] = s.[object_id] - GROUP BY t.name - ,s.partition_number - ,s.row_count; - '; - SET @sql = REPLACE(@sql,'@dbName',@dbName) - EXEC (@sql) + CREATE TABLE #t ( + name sysname + , partition_number int + , row_count int + ); + INSERT #t + EXEC jt.GetPartitionRowTallies @dbName = @dbName; + SELECT name,row_count = SUM(row_count) + FROM #t + GROUP BY name; END \ No newline at end of file