Solved

SQL Statistics

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dice Roll Probabilities 3 97
listing all functions in JavaScript 19 200
What is difference between Expression and Statement in terms of Programming Language (C#) 6 85
Not needed 13 110
A short article about a problem I had getting the GPS LocationListener working.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 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