提供行计数和表名的脚本

时间:2012-03-31 09:37:57

标签: sql sql-server

也许你很容易说我如何提供表名和行数?

伪SQL:

for "select tablename from system.Tables" into :tablename
  execute "select count(*) from ? into ?" using :tablename, :count
  return row(:tablename, :count)
end for

你能告诉我用T-SQL向我展示这个脚本吗?

7 个答案:

答案 0 :(得分:54)

如果您使用的是SQL Server 2005或更新(遗憾的是您没有指定您正在使用的SQL Server的版本),此查询应该会给您那个信息:

SELECT 
    TableName = t.NAME,
    TableSchema = s.Name,
    RowCounts = p.rows
FROM 
    sys.tables t
INNER JOIN 
    sys.schemas s ON t.schema_id = s.schema_id
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
WHERE 
    t.is_ms_shipped = 0
GROUP BY
    t.NAME, s.Name, p.Rows
ORDER BY 
    s.Name, t.Name

这会产生类似的输出(这来自AdventureWorks):

TableName       TableSchema      RowCounts
AWBuildVersion    dbo                  1
DatabaseLog       dbo               1597
ErrorLog          dbo                  0
Department        HumanResources      16
Employee          HumanResources     290
JobCandidate      HumanResources      13
Address           Person           19614
AddressType       Person               6
... and so on......

答案 1 :(得分:2)

-- Shows all user tables and row counts for the current database 
-- Remove OBJECTPROPERTY function call to include system objects 
SELECT o.NAME,
  i.rowcnt 
FROM sysindexes AS i
  INNER JOIN sysobjects AS o ON i.id = o.id 
WHERE i.indid < 2  AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
ORDER BY o.NAME

答案 2 :(得分:1)

exec sp_MSForEachTable 'SELECT ''?'' as TableName, COUNT(*) as Rows FROM ?'

答案 3 :(得分:1)

我已经使用CTE调整了marc_c的答案,并选择显示您正在使用的架构。

应与 SQL Serve 2005及更新版一起使用。

WITH CountRowsInTables (Table_Name, Table_Schema, Row_Counts) AS
(
SELECT 
TableName = t.Name,
TableSchema = s.Name,
RowCounts = p.Rows
    FROM 
        sys.tables t
    INNER JOIN 
        sys.schemas s ON t.schema_id = s.schema_id
    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
    WHERE 
        t.is_ms_shipped = 0
    GROUP BY
        s.Name, t.Name, p.Rows
)

SELECT Table_name, Table_Schema, Row_Counts
    FROM CountRowsInTables
    WHERE Table_Schema = 'Pick_Schema_to_display'; 

答案 4 :(得分:1)

SELECT 
t.NAME AS TableName, p.[Rows] FROM 
sys.tables t INNER JOIN
sys.partitions p ON t.object_id = p.OBJECT_ID GROUP BY 
t.NAME, p.[Rows] ORDER BY    t.NAME

答案 5 :(得分:0)

试试这个

-- drop table #tmpspace
create table #tmpspace (
        name sysname
        , rows int
        , reserved varchar(50)
        , data varchar(50)
        , index_size varchar(50)
        , unused varchar(50)
        )

dbcc updateusage(0) with NO_INFOMSGS

exec sp_msforeachtable 'insert #tmpspace exec sp_spaceused ''?'''

select * from #tmpspace
order by convert(int, substring(reserved, 1, charindex(' ', reserved))) desc, rows desc, name

也适用于sql2000。

dbcc updateusage可能需要一些时间,但结果将是100%实际。如果您需要速度超过准确性,请跳过它。

答案 6 :(得分:0)

这对我有用:

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