Solved

SQL Statistics

Posted on 2004-04-26
5
791 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

19 Experts available now in Live!

Get 1:1 Help Now