all groups > sql server programming > october 2003 >
You're in the

sql server programming

group:

how to report tables names with record counts


how to report tables names with record counts JJ Wang
10/5/2003 11:36:23 PM
sql server programming:
hi,

How can I generate a list of table names along with their
record counts from a database?

Thanks a lot.

thank you all so kindly for the extremely helpful info. JJ Wang
10/6/2003 9:27:04 AM

[quoted text, click to view]
Re: how to report tables names with record counts Anith Sen
10/6/2003 9:27:52 AM
Refer to:
http://groups.google.com/groups?selm=%2394T2eYvCHA.1624%40TK2MSFTNGP10

--
- Anith
( Please reply to newsgroups only )

Re: how to report tables names with record counts Jacco Schalkwijk
10/6/2003 10:10:06 AM
http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=629

for a script that gives the row counts and quite a bit of other information
about your tables and columns.

--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

how to report tables names with record counts Greg
10/6/2003 1:10:50 PM
You could also do something like this.

CREATE TABLE #TableCount (CNT INT, TableName VARCHAR(100))
INSERT INTO #TableCount (CNT, TableName)
EXEC
sp_MSForEachTable @command1 = "SELECT COUNT(*) AS CNT, '?'
AS TableName FROM ?"

SELECT * FROM #TableCount
DROP TABLE #TableCount


Greg


[quoted text, click to view]
Re: how to report tables names with record counts SriSamp
10/6/2003 2:48:04 PM
Here is one method:
====
DECLARE tablesList CURSOR FOR
-- Cursor to select all the tables from a database
SELECT table_name FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
ORDER BY table_name

-- Scratch variables
DECLARE @tableCount INT
DECLARE @lcTableName VARCHAR(100)
DECLARE @sqlString NVARCHAR(1000)

-- Table that stores the output
CREATE TABLE #tableCounts (tableName VARCHAR(100), recordCount INT)

OPEN tablesList
FETCH NEXT FROM tablesList INTO @lcTableName
WHILE (@@FETCH_STATUS = 0)
BEGIN
-- Form the string to determine the count. Execute it and store the list
in the temporary
-- table
SET @sqlString = 'SELECT @rowCount = COUNT(*) FROM ' + @lcTableName
EXEC sp_executesql @sqlString, N'@rowCount INT OUTPUT', @tableCount
OUTPUT
INSERT INTO #tableCounts VALUES (@lcTableName, @tableCount)

-- Fetch the next table in the list
FETCH NEXT FROM tablesList INTO @lcTableName
END

-- Cleanup the cursor
CLOSE tablesList
DEALLOCATE tablesList

-- Display data from the table and cleanup
SELECT * FROM #tableCounts
DROP TABLE #tableCounts
=====

Another option is to use SQLDMO from a COM compliant application (even SQL
Server). SQLDMO has certain methods that fetch this value directly.
--
HTH,
SriSamp
Please reply to the whole group only!
http://www32.brinkster.com/srisamp

[quoted text, click to view]

AddThis Social Bookmark Button