37

我试图通过应用程序(由其他人开发)确定哪些表受到数据更新的影响。数据库中有 200 多个表,我想避免检查每个表的更改。

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

select table_name from information_schema.tables

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

4

4 回答 4

74
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

看到这个:

于 2012-10-18T08:46:38.743 回答
4

获取数据库中的所有表:

select * from INFORMATION_SCHEMA.TABLES

获取数据库中的所有列:

select * from INFORMATION_SCHEMA.columns

获取数据库中的所有视图:

select * from INFORMATION_SCHEMA.TABLES where table_type = 'view'
于 2013-12-06T06:20:32.413 回答
0

为完整性而发布。

如果您正在寻找所有数据库中所有表的行数(这是我正在寻找的),那么我发现这个这个组合可以工作。不知道它是否是最优的:

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
于 2014-06-02T12:07:54.483 回答
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
于 2018-05-25T05:22:08.457 回答