Solved

SQL Statistics

Posted on 2004-04-26
5
792 Views
Last Modified: 2013-11-13
I want to retrieve the statistics on a table and store them in a different table for historical purposes.
I need things like Row count etc but I do not want to do a count(*) if these values are stored somewhere.
I know one can use DBCC SHOW_STATISTICS but does anyone have any code that simplifies this - in other words loops through all the user tables and get the statistics for them?
or does anyone know where these values are stored.

Thanks
Kathy
0
Comment
Question by:azrakdragon
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:_ys_
ID: 10920142
A basic query would be:

SELECT      name, rows
FROM      sysIndexes
WHERE      indid = 0

This returns the table-name and rowcount. Keep in mind that the row count is not guaranteed to be updated real time.
0
 

Author Comment

by:azrakdragon
ID: 10935955
Hi ys

Would this only apply to tables that have indexes on them??
0
 
LVL 9

Expert Comment

by:_ys_
ID: 10936510
I can't imagine a table that doesn't have at least one. I mean when you define a PRIMARY KEY an index (usually clustered) is created on your behalf. Unless your tables don't have a PRIMARY KEY defined, and I would advise against it.
0
 

Author Comment

by:azrakdragon
ID: 10937135
I understand what you are saying, perhaps I should have mentioned I want this information for a data warehouse. Unique columns are defined by Unique indexes but I cannot guarantee that every single table has an index especially our aggregated tables.

What I am ultimately looking for is the information displayed on the "Taskpad" view under Table Info on a database. Surely this kind of information is calculated and stored somewhere or is it calculated on the fly??
Where or how can I get this kind of information.

Regards
Kathy
0
 
LVL 9

Accepted Solution

by:
_ys_ earned 500 total points
ID: 10937509
In that case COUNT(*) is what you gotta do.

If you're using MS SQL, here's a fully functional script:

---------------x---------------
CREATE TABLE dbo.table_count
(
       table_name NVARCHAR(128) NOT NULL PRIMARY KEY
      ,row_count INTEGER NOT NULL
)

INSERT      dbo.table_count
EXEC      sp_msForEachTable 'SELECT ''?'', COUNT(*) FROM ?'

SELECT      *
FROM      dbo.table_count

DROP TABLE dbo.table_count
---------------x---------------
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to open Waze.com/livemap from address saved in DB? 26 178
Hide vba in gp 7 83
Currency Conversion? 1 73
Is doing tutor.com teaching in my situation advisable? 2 57
Introduction This question got me thinking... (http://www.experts-exchange.com/questions/28707487/GLOBALS.html) Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now