查询列出数据库中每个表中的logging数

如何列出数据库中每个表的行数。 相当于一些

select count(*) from table1 select count(*) from table2 ... select count(*) from tableN 

我会发布一个解决scheme,但其他方法是受欢迎的

如果你正在使用SQL Server 2005或更高版本,你也可以使用这个:

 SELECT t.NAME AS TableName, i.name as indexName, p.[Rows], sum(a.total_pages) as TotalPages, sum(a.used_pages) as UsedPages, sum(a.data_pages) as DataPages, (sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, (sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, (sum(a.data_pages) * 8) / 1024 as DataSpaceMB FROM sys.tables t INNER JOIN sys.indexes i ON t.OBJECT_ID = i.object_id INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id WHERE t.NAME NOT LIKE 'dt%' AND i.OBJECT_ID > 255 AND i.index_id <= 1 GROUP BY t.NAME, i.object_id, i.index_id, i.name, p.[Rows] ORDER BY object_name(i.object_id) 

在我看来,它比sp_msforeachtable输出更容易处理。

我在http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=21021find了一个帮助我的代码片段:;

 select t.name TableName, i.rows Records from sysobjects t, sysindexes i where t.xtype = 'U' and i.id = t.id and i.indid in (0,1) order by TableName; 

要在SQL Management Studio中获取这些信息,请右键单击数据库,然后selectReports – > Standard Reports – > Table Usage by Table。

 SELECT T.NAME AS 'TABLE NAME', P.[ROWS] AS 'NO OF ROWS' FROM SYS.TABLES T INNER JOIN SYS.PARTITIONS P ON T.OBJECT_ID=P.OBJECT_ID; 

如此处所示,这将返回正确的计数,其中使用元数据表的方法将仅返回估计值。

  CREATE PROCEDURE ListTableRowCounts AS BEGIN SET NOCOUNT ON CREATE TABLE #TableCounts ( TableName VARCHAR(500), CountOf INT ) INSERT #TableCounts EXEC sp_msForEachTable 'SELECT PARSENAME(''?'', 1), COUNT(*) FROM ? WITH (NOLOCK)' SELECT TableName , CountOf FROM #TableCounts ORDER BY TableName DROP TABLE #TableCounts END GO 
 sp_MSForEachTable 'DECLARE @t AS VARCHAR(MAX); SELECT @t = CAST(COUNT(1) as VARCHAR(MAX)) + CHAR(9) + CHAR(9) + ''?'' FROM ? ; PRINT @t' 

输出:

在这里输入图像描述

首先想到的是使用sp_msForEachTable

 exec sp_msforeachtable 'select count(*) from ?' 

虽然没有列出表名,所以可以扩展到

 exec sp_msforeachtable 'select parsename(''?'', 1), count(*) from ?' 

这里的问题是,如果数据库有超过100个表,您将得到以下错误信息:

该查询已超出结果集中可显示在结果网格中的最大数量。 只有前100个结果集显示在网格中。

所以我最终使用表variables来存储结果

 declare @stats table (n sysname, c int) insert into @stats exec sp_msforeachtable 'select parsename(''?'', 1), count(*) from ?' select * from @stats order by c desc 

幸运的是,SQL Serverpipe理工作室给你一个关于如何做到这一点的提示。 做这个,

  1. 启动SQL Server跟踪并打开正在执行的活动(如果您不是独自一人,将您的loginID过滤,并将应用程序名称设置为Microsoft SQL Server Management Studio),暂停跟踪并放弃迄今为止logging的任何结果;
  2. 然后,右键单击一个表并从popup菜单中select属性;
  3. 再次开始追踪;
  4. 现在在SQL Server Management Studio中select左边的存储属性项目;

暂停跟踪并查看微软产生的TSQL。

在可能的最后一个查询中,您将看到一个以exec sp_executesql N'SELECT开头的语句

当您将执行的代码复制到Visual Studio中时,您会注意到此代码会生成微软工程师用来填充属性窗口的所有数据。

当你对这个查询进行适度的修改时,你会得到如下的结果:

 SELECT SCHEMA_NAME(tbl.schema_id)+'.'+tbl.name as [table], --> something I added p.partition_number AS [PartitionNumber], prv.value AS [RightBoundaryValue], fg.name AS [FileGroupName], CAST(pf.boundary_value_on_right AS int) AS [RangeType], CAST(p.rows AS float) AS [RowCount], p.data_compression AS [DataCompression] FROM sys.tables AS tbl INNER JOIN sys.indexes AS idx ON idx.object_id = tbl.object_id and idx.index_id < 2 INNER JOIN sys.partitions AS p ON p.object_id=CAST(tbl.object_id AS int) AND p.index_id=idx.index_id LEFT OUTER JOIN sys.destination_data_spaces AS dds ON dds.partition_scheme_id = idx.data_space_id and dds.destination_id = p.partition_number LEFT OUTER JOIN sys.partition_schemes AS ps ON ps.data_space_id = idx.data_space_id LEFT OUTER JOIN sys.partition_range_values AS prv ON prv.boundary_id = p.partition_number and prv.function_id = ps.function_id LEFT OUTER JOIN sys.filegroups AS fg ON fg.data_space_id = dds.data_space_id or fg.data_space_id = idx.data_space_id LEFT OUTER JOIN sys.partition_functions AS pf ON pf.function_id = prv.function_id 

现在,查询并不完美,您可以更新它来满足您可能遇到的其他问题,重要的是,您可以使用Microsoft的知识通过执行您感兴趣的数据来获取大多数问题,并追踪使用分析器生成的TSQL。

我喜欢以为MS工程师知道SQL服务器是如何工作的,它将生成TSQL,它可以在你使用的SSMS版本上使用的所有项目都能正常工作,所以它在各种版本的prerviouse,current和未来。

记住,不要只是复制,试着理解它,否则你可能会得到错误的解决scheme。

沃尔特

在Azure SQL上,我所接受的答案并不适用于我,这是一个非常快速的做法,完全符合我的要求:

 select t.name, s.row_count from sys.tables t join sys.dm_db_partition_stats s ON t.object_id = s.object_id and t.type_desc = 'USER_TABLE' and t.name not like '%dss%' and s.index_id = 1 order by s.row_count desc 

如果你使用MySQL> 4.x你可以使用这个:

 select TABLE_NAME, TABLE_ROWS from information_schema.TABLES where TABLE_SCHEMA="test"; 

请记住,对于某些存储引擎,TABLE_ROWS是一个近似值。

最快的方法来查找所有表中的行数在Reflece( http://www.codeproject.com/Tips/811017/Fastest-way-to-find-row-count-of-all-tables-in-SQL

 SELECT T.name AS [TABLE NAME], I.rows AS [ROWCOUNT] FROM sys.tables AS T INNER JOIN sys.sysindexes AS I ON T.object_id = I.id AND I.indid < 2 ORDER BY I.rows DESC` 

我认为最短,最快,最简单的方法是:

 SELECT object_name(object_id) AS [Table], SUM(row_count) AS [Count] FROM sys.dm_db_partition_stats WHERE --object_schema_name(object_id) = 'dbo' AND index_id < 2 GROUP BY object_id 

你可以试试这个:

 SELECT OBJECT_SCHEMA_NAME(ps.object_Id) AS [schemaname], OBJECT_NAME(ps.object_id) AS [tablename], row_count AS [rows] FROM sys.dm_db_partition_stats ps WHERE OBJECT_SCHEMA_NAME(ps.object_Id) <> 'sys' AND ps.index_id < 2 ORDER BY OBJECT_SCHEMA_NAME(ps.object_Id), OBJECT_NAME(ps.object_id) 

这种方法使用string连接来dynamic生成一个包含所有表及其计数的语句,如原始问题中给出的示例:

  SELECT COUNT(*) AS Count,'[dbo].[tbl1]' AS TableName FROM [dbo].[tbl1] UNION ALL SELECT COUNT(*) AS Count,'[dbo].[tbl2]' AS TableName FROM [dbo].[tbl2] UNION ALL SELECT... 

最后这是用EXEC

 DECLARE @cmd VARCHAR(MAX)=STUFF( ( SELECT 'UNION ALL SELECT COUNT(*) AS Count,''' + QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) + ''' AS TableName FROM ' + QUOTENAME(t.TABLE_SCHEMA) + '.' + QUOTENAME(t.TABLE_NAME) FROM INFORMATION_SCHEMA.TABLES AS t WHERE TABLE_TYPE='BASE TABLE' FOR XML PATH('') ),1,10,''); EXEC(@cmd); 
 select T.object_id, T.name, I.indid, I.rows from Sys.tables T left join Sys.sysindexes I on (I.id = T.object_id and (indid =1 or indid =0 )) where T.type='U' 

这里indid=1表示一个CLUSTERED索引, indid=0表示一个HEAP