MS SQL 2008 - 获取数据库中的所有表名及其行数

时间:2012-10-18 08:44:14

标签: sql-server sql-server-2008 row

我正在尝试通过应用程序(由其他人开发)来识别哪些表受数据更新的影响。数据库中有200多个表,我想避免检查每个表的变化。

还有其他方法可以列出所有表名及其行数吗?

select table_name from information_schema.tables

列出我的数据库中的所有表格购买如何包含行数?

4 个答案:

答案 0 :(得分:73)

SELECT sc.name +'.'+ ta.name TableName
 ,SUM(pa.rows) RowCnt
 FROM sys.tables ta
 INNER JOIN sys.partitions pa
 ON pa.OBJECT_ID = ta.OBJECT_ID
 INNER JOIN sys.schemas sc
 ON ta.schema_id = sc.schema_id
 WHERE ta.is_ms_shipped = 0 AND pa.index_id IN (1,0)
 GROUP BY sc.name,ta.name
 ORDER BY SUM(pa.rows) DESC

见:

答案 1 :(得分:4)

获取数据库中的所有表:

select * from INFORMATION_SCHEMA.TABLES

获取数据库中的所有列:

select * from INFORMATION_SCHEMA.columns

获取db中的所有视图:

select * from INFORMATION_SCHEMA.TABLES where table_type = 'view'

答案 2 :(得分:0)

发布完整性。

如果您要查找所有数据库中所有表的行数(我正在寻找),那么我发现了这一点thisthis的组合可以发挥作用。不知道它是否是最佳的:

SET NOCOUNT ON
DECLARE @AllTables table (DbName sysname,SchemaName sysname, TableName sysname, RowsCount int )
DECLARE
     @SQL nvarchar(4000)
SET @SQL='SELECT ''?'' AS DbName, s.name AS SchemaName, t.name AS TableName, p.rows AS RowsCount FROM [?].sys.tables t INNER JOIN sys.schemas s ON t.schema_id=s.schema_id INNER JOIN [?].sys.partitions p ON p.OBJECT_ID = t.OBJECT_ID'

INSERT INTO @AllTables (DbName, SchemaName, TableName, RowsCount)
    EXEC sp_msforeachdb @SQL
SET NOCOUNT OFF
SELECT DbName, SchemaName, TableName, SUM(RowsCount), MIN(RowsCount), SUM(1)
FROM @AllTables
WHERE RowsCount > 0
GROUP BY DbName, SchemaName, TableName
ORDER BY DbName, SchemaName, TableName

答案 3 :(得分:0)

  

试试这个简单快捷的

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