C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » SQL »

Select Total Number of records in the Database.


Posted Date: 28 Jul 2009    Resource Type: Code Snippets    Category: SQL
Author: MohanMember Level: Diamond    
Rating: 1 out of 5Points: 4



This Coding is used to count all the table records in tablewise and count the records in overall Database.

It's Simple query to count the records in each table.



--Specify the name of the database to count in the following line
USE master;
GO

SELECT sysobj.name AS "Table Name", sysind.rowcnt AS "Row Count"
FROM sysobjects sysobj, sysindexes sysind
WHERE sysobj.id = sysind.id
AND indid IN(0,1)
--This specifies 'user' databases only
AND xtype = 'u'
--This omits the diagrams table of the database
--You may find other system tables will need to be ommitted,
--you would just name them all here using the <> operator
--i.e. o.name <> dtproperties, o.name <> 'sysdiagrams'
AND sysobj.name <> 'sysdiagrams'

--You could also look further into filtering out temp tables,
--or user specified tables
ORDER BY sysind.rowcnt DESC
--the results by 'Row Count' Descending
COMPUTE SUM(sysind.rowcnt);
--overall count of the database
GO




Responses

Author: Prashant Mishra    17 Sep 2009Member Level: Bronze   Points : 2
Nice one:)

Also you can try try this on.

this is availble on dotnet spider "Tables' name with record count - SQL Server" .

CREATE TABLE #Table_Count
(
TableName VARCHAR(MAX),
RecordCount BIGINT
)
GO
INSERT #Table_Count
EXEC SP_MsForEachTable 'SELECT ''?'', COUNT(1) FROM ?'
GO
SELECT * FROM #Table_Count WHERE RecordCount > 0 ORDER BY RecordCount DESC
GO
DROP TABLE #Table_Count


Author: Mohan    17 Sep 2009Member Level: Diamond   Points : 2
Hi Prashant,

Thanks for sharing your knowledge with this resource.

I found one performance issue with your given SP.

My Database contains nearly 650 tables.

My query returns the records within 20 seconds. But Your given SP it takes 10 mins for completion.

So Better you dont follow that SP..


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Count the records in database  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Reading a CSV using OPENROWSET Function in Sql server 2005
Previous Resource: Create Table Using Select in Sql Server
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use