Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Statistics

Posted on 2004-04-26
5
Medium Priority
?
801 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 1500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Screencast - Getting to Know the Pipeline

916 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