{"id":470,"date":"2013-08-05T10:53:46","date_gmt":"2013-08-05T15:53:46","guid":{"rendered":"https:\/\/mkncreations.com\/site\/?p=470"},"modified":"2015-09-15T20:16:20","modified_gmt":"2015-09-16T01:16:20","slug":"tsql-show-all-tables-with-rows","status":"publish","type":"post","link":"https:\/\/mkncreations.com\/site\/2013\/08\/tsql-show-all-tables-with-rows\/","title":{"rendered":"TSQL &#8211; Show all tables with rows"},"content":{"rendered":"<p>If you would like to see the row count of each table in a database and you don&#8217;t want to go through every single table here is how you do it:<\/p>\n<blockquote><p>\nSELECT o.name AS [Table Name]<br \/>\n, ddps.row_count AS [Row Count]<\/p>\n<p>FROM sys.indexes AS i<br \/>\nINNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID<br \/>\nINNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID = ddps.OBJECT_ID<br \/>\nAND i.index_id = ddps.index_id <\/p>\n<p>WHERE i.index_id < 2 \nAND o.is_ms_shipped = 0 \n\nORDER BY row_count DESC, o.name\n<\/p><\/blockquote>\n<p>Below you can find an alternative method which takes longer to execute.<\/p>\n<p><strong>Source:<\/strong> http:\/\/www.sqlmatters.com\/Articles\/Listing all tables in a database and their row counts and sizes.aspx<\/p>\n<blockquote><p>\nCREATE TABLE #RowCounts(NumberOfRows BIGINT,TableName VARCHAR(128))<\/p>\n<p>EXEC sp_MSForEachTable &#8216;INSERT INTO #RowCounts<br \/>\n                        SELECT COUNT_BIG(*) AS NumberOfRows,<br \/>\n                        &#8221;?&#8221; as TableName FROM ?&#8217;<\/p>\n<p>SELECT   TableName,NumberOfRows<br \/>\nFROM     #RowCounts<br \/>\nORDER BY NumberOfRows DESC,TableName<\/p>\n<p>DROP TABLE #RowCounts\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>If you would like to see the row count of each table in a database and you don&#8217;t want to go through every single table&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,12],"tags":[109,110,107,50,78,108,106,49],"class_list":["post-470","post","type-post","status-publish","format-standard","hentry","category-how_to","category-sql","tag-count","tag-data","tag-rows","tag-sql-2","tag-sql2008","tag-table","tag-tables","tag-tsql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/470"}],"collection":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":12,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":276461,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/posts\/470\/revisions\/276461"}],"wp:attachment":[{"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mkncreations.com\/site\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}