views:

156

answers:

4

I need to know how many records are in each table in a particular database. I don't care what is in the record, they will all need to be counted.

I already know how to do this for a single table as SELECT Count(1) FROM [my_table_name], but this gets a little redundant for 200+ tables.

+5  A: 

Try this: (Very fast method though potentially less precise.)

SELECT   so.name, MAX(si.rows) 
FROM     sysobjects so
JOIN     sysindexes si ON so.xtype = 'U' 
AND      si.id = OBJECT_ID(so.name) 
GROUP BY so.name 
ORDER BY 2 DESC

That's one method of many. See here for several other options.

Paul Sasik
Cite your source, Paul: http://sqlserver2000.databases.aspfaq.com/how-do-i-get-a-list-of-sql-server-tables-and-their-row-counts.html
Matt
@Matt: You caught me mid edit. i always do.
Paul Sasik
this is only an estimate, not 100% exact, and used awful ancient (yucky) join syntax.
KM
@KM: Cleaned up the syntax. This is a very fast method though. Perfect if you want a quick answer and not overload your server with metadata calculation.
Paul Sasik
I had originally accepted astander's answer, but this does the same thing 3x faster. Thanks!
Heather
This method didn't seem to return anything on my database, so I had a look and it's got problems because of schemas since OBJECT_ID(so.name) returns NULL on all my objects in schemas. OBJECT_ID(so.name) should probably be simply so.id, and you should GROUP BY OBJECT_SCHEMA_NAME(so.id), since the same table name can reside in different schemas.
Cade Roux
@Cade Roux: On my DB server I've only got one schema since there is only one user besides `sa` (me). That is good to know regarding the possibility of multiple schemas - that was something I didn't know about SQL Server 2005 until I Googled it.
Heather
+3  A: 

From How do I get a list of SQL Server tables and their row counts?

DECLARE @SQL VARCHAR(255) 
SET @SQL = 'DBCC UPDATEUSAGE (' + DB_NAME() + ')' 
EXEC(@SQL) 

CREATE TABLE #foo 
( 
    tablename VARCHAR(255), 
    rc INT 
) 

INSERT #foo 
    EXEC sp_msForEachTable 
        'SELECT PARSENAME(''?'', 1), 
        COUNT(*) FROM ?' 

SELECT tablename, rc 
    FROM #foo 
    ORDER BY rc DESC 

DROP TABLE #foo 
astander
This is exactly what I was looking for, thank you!
Heather
+1  A: 

As has already been posted, if it was a one off thing I'd probably use sp_msforeachtable. However, it's an undocument system procedure so if that's a problem, then you can easily do it like this:

DECLARE @SQL VARCHAR(MAX)
SET @SQL = ''

SELECT @SQL = 
    @SQL + 'SELECT ''' + TABLE_NAME + ''' AS TableName, COUNT(*) AS RecordCount 
            FROM [' + TABLE_NAME + '];' + CHAR(13)
FROM INFORMATION_SCHEMA.TABLES

EXECUTE (@SQL)
AdaTheDev
+1  A: 

I'm sure you can get something better than this but this should do in a pinch. For the record, sp_spaceused is your friend in queries such as this!

DECLARE @tbls table (TableID int identity, TableName varchar(max))

INSERT INTO @tbls
SELECT t1.name
FROM sysobjects t1
INNER JOIN sysindexes t2
ON t1.id = t2.id
WHERE t2.indid <= 1
AND t2.rows > 0
AND OBJECTPROPERTY(t1.id,'IsUserTable') = 1

DECLARE @tblcount int

SELECT @tblcount=COUNT(1)
FROM @tbls

DECLARE @results table (name varchar(max), [rows] bigint, reserved varchar(max), data varchar(max), index_size varchar(max), unused varchar(max))

DECLARE @counter int
SET @counter = 0
DECLARE @tblname varchar(max)

while @counter < @tblcount
begin
    SELECT @tblname=TableName FROM @tbls WHERE TableID=@counter+1

    INSERT INTO @results
    exec sp_spaceused @tblname

    SET @counter = @counter+1
end

SELECT * FROM @results
Jaxidian
haha, and then this dev backs away from the database... ;-)
Jaxidian