Introduction
How many records are there in your database? You might be thinking that you need to write a gigantic SQL statement for that. This article will demonstrate to you, how easily you can do it.
Isn't it interesting? The following code will allow you to count all of the records contained within any single user database you have created.
Using the Code
To use this code is very simple. Just open a query analyzer, put the code into the SQL editor and run it. If you want, you can make it as StoredProcedure / view or whatever you want.
SELECT SYS_OBJ.NAME AS "TABLE NAME"
, SYS_INDX.ROWCNT AS "ROW COUNT"
FROM SYSOBJECTS SYS_OBJ, SYSINDEXES SYS_INDX
WHERE SYS_INDX.ID = SYS_OBJ.ID
AND INDID IN(0,1)
AND XTYPE = 'U'
AND SYS_OBJ.NAME <> 'SYSDIAGRAMS'
ORDER BY SYS_INDX.rowcnt DESC
COMPUTE SUM(SYS_INDX.ROWCNT)
GO
Note
[sysobjects
]
Contains one row for each object (constraint, default, log, rule, stored procedure, and so on) created within a database.
[sys.sysindexes
]
Contains one row for each index and table in the current database. XML indexes are not supported in this view. Partitioned tables and indexes are not fully supported in this view; use the sys.indexes
catalog view instead.
Reference: MSDN
History
- 20th July, 2009: Initial post