2012-03-31 69 views
16

也許你會輕鬆地說我如何提供表名和行數?提供行數和表名的腳本

僞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這個腳本?

回答

48

如果你的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...... 
+0

Arent't那些rowcounts只估計? – 2012-03-31 11:27:37

+0

@a_horse_with_no_name:不是**估計** - 而是近似值。行數會定期更新 - 但如果您執行了大量插入或刪除操作,則在任何給定時間它們都不是100%準確的,這是正確的。但是,在性能和執行時間方面,在具有相當數量的行的體面大小的數據庫中對所有表執行'SELECT COUNT(*)'操作只會是** **。 – 2012-03-31 11:29:04

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 
+0

我會嘗試所有的解決方案!謝謝! – durumdara 2012-03-31 09:53:53

1
exec sp_MSForEachTable 'SELECT ''?'' as TableName, COUNT(*) as Rows FROM ?' 
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%。如果您需要速度超過準確性,請跳過它。

1

我已經調整了marc_c與CTE的答案,並顯示它只顯示你之後的模式。

應與一起使用SQL Server 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'; 
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