Click here to Skip to main content
Licence CPOL
First Posted 20 Jul 2009
Views 12,728
Bookmarked 45 times

How Many Records Are There in your Database?

By | 20 Jul 2009 | Article
How many records are there in your database?
 
Part of The SQL Zone sponsored by
See Also

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.

-- =============================================
-- 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)

About the Author

Md. Marufuzzaman

Architect

Bangladesh Bangladesh

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberhadi552619:11 16 Aug '11  
GeneralRe: My vote of 5 PinmvpMd. Marufuzzaman22:50 5 Oct '11  
GeneralMy vote of 5 PinmemberAslam_Iqbal3:20 13 Jan '11  
GeneralRe: My vote of 5 PinmvpMd. Marufuzzaman22:51 5 Oct '11  
GeneralMy vote of 5 PinmemberTasnia.Maruf0:24 20 Sep '10  
GeneralRe: My vote of 5 PinmvpMd. Marufuzzaman18:23 1 Oct '10  
GeneralMy vote of 1 PinmemberPSU Steve8:54 23 Jul '09  
GeneralRe: My vote of 1 PingroupMd. Marufuzzaman5:13 26 Jul '09  
GeneralI think this just counts the number of rows in a specific index... PinmemberJohn_Crocker21:59 20 Jul '09  
AnswerRe: I think this just counts the number of rows in a specific index... PinmemberMd. Marufuzzaman0:19 21 Jul '09  
GeneralRe: I think this just counts the number of rows in a specific index... Pinmemberje_gonzalez18:53 21 Jul '09  
GeneralRe: I think this just counts the number of rows in a specific index... PingroupMd. Marufuzzaman5:14 26 Jul '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Jul 2009
Article Copyright 2009 by Md. Marufuzzaman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid