Click here to Skip to main content
15,886,788 members
Articles / Database Development / SQL Server / SQL Server 2008

How Many Records Are There in your Database?

Rate me:
Please Sign up or sign in to vote.
4.60/5 (31 votes)
20 Jul 2009CPOL 40.7K   47   12
How many records are there in your database?

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.

SQL
-- =============================================
-- Author:        <Author,,Md. Marufuzzaman>
-- Create date: <Create Date,,>
-- Description:    <Description,,>
-- =============================================

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) --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 omitted,
 AND SYS_OBJ.NAME <> 'SYSDIAGRAMS'

ORDER BY SYS_INDX.rowcnt DESC --I found it more useful to display 
--The following line adds up all the rowcount results and places
--the final result into a separate column [below the first resulting table]
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
hadi552616-Aug-11 19:11
hadi552616-Aug-11 19:11 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman5-Oct-11 22:50
professionalMd. Marufuzzaman5-Oct-11 22:50 
GeneralMy vote of 5 Pin
Аslam Iqbal13-Jan-11 3:20
professionalАslam Iqbal13-Jan-11 3:20 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman5-Oct-11 22:51
professionalMd. Marufuzzaman5-Oct-11 22:51 
GeneralMy vote of 5 Pin
Tasnia.Maruf20-Sep-10 0:24
Tasnia.Maruf20-Sep-10 0:24 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman1-Oct-10 18:23
professionalMd. Marufuzzaman1-Oct-10 18:23 
GeneralMy vote of 1 Pin
PSU Steve23-Jul-09 8:54
professionalPSU Steve23-Jul-09 8:54 
GeneralRe: My vote of 1 Pin
Md. Marufuzzaman26-Jul-09 5:13
professionalMd. Marufuzzaman26-Jul-09 5:13 
GeneralI think this just counts the number of rows in a specific index... Pin
Argyle4Ever20-Jul-09 21:59
Argyle4Ever20-Jul-09 21:59 
AnswerRe: I think this just counts the number of rows in a specific index... Pin
Md. Marufuzzaman21-Jul-09 0:19
professionalMd. Marufuzzaman21-Jul-09 0:19 
GeneralRe: I think this just counts the number of rows in a specific index... Pin
je_gonzalez21-Jul-09 18:53
je_gonzalez21-Jul-09 18:53 
GeneralRe: I think this just counts the number of rows in a specific index... Pin
Md. Marufuzzaman26-Jul-09 5:14
professionalMd. Marufuzzaman26-Jul-09 5:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.