Solved

SQL Server 2008 - Get general stats on tables, row counts etc

Posted on 2013-06-19
1
417 Views
Last Modified: 2013-06-19
How can I query or does someone have an SP that will output general stats on a database,
like

Table Name , Row Count  , File Size,, number of transactions  - db file size etc.
thx
0
Comment
Question by:JElster
1 Comment
 
LVL 11

Accepted Solution

by:
Guru Ji earned 500 total points
ID: 39259905
You can run the following code against any database, will show u entire table structure with
Row Count, Table Name, ModifiedRows, LastUpdated Date,

select
schemas.name as table_schema,
tbls.name as table_name,
i.name as index_name,
i.id as table_id,
i.indid as index_id,
i.rowmodctr as modifiedRows,
(select max(rowcnt) from sysindexes i2 where i.id = i2.id and i2.indid < 2) as rowcnt,
convert(DECIMAL(18,8), convert(DECIMAL(18,8),i.rowmodctr) / convert(DECIMAL(18,8),(select max(rowcnt) from sysindexes i2 where i.id = i2.id and i2.indid < 2))) as ModifiedPercent,
stats_date( i.id, i.indid ) as lastStatsUpdate
from sysindexes i
inner join sysobjects tbls on i.id = tbls.id
inner join sysusers schemas on tbls.uid = schemas.uid
inner join information_schema.tables tl
on tbls.name = tl.table_name
and schemas.name = tl.table_schema
and tl.table_type='BASE TABLE'
where 0 < i.indid and i.indid < 255
and table_schema <> 'sys'
and i.rowmodctr <> 0
and i.status not in (8388704,8388672)
and (select max(rowcnt) from sysindexes i2 where i.id = i2.id and i2.indid < 2) > 0

Open in new window


In order to get Table size run the following code which shows table size of all the tables in your database

CREATE TABLE #temp
       (
       [name] nvarchar(128),
       [rows] char(11),
       [reserved] varchar(18),
       [data] varchar(18),
       [index_size] varchar(18),
       [unused] varchar(18)
       )
 
INSERT INTO #temp
EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?' "
 
SELECT
       [name] AS TableName,
       CAST([rows] AS int) AS NumOfRows,
       CAST(REPLACE([reserved],' KB','') AS int) AS [TotalKB]
FROM
       #temp
ORDER BY 3 DESC
 
SELECT SUM(CAST(REPLACE([reserved],' KB','') AS int)) AS TotalKB_data FROM #temp
 
DROP TABLE #temp

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

765 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