Solved

find the table size using sp_spaceused

Posted on 2011-09-14
6
443 Views
Last Modified: 2012-05-12
Hi,

We have prod server (X1) and this server contains around 250 DB's. and in all DB's there is one common table called 'MyTable' (but number of rows differ from DB to DB). I want to find out table size,rowcount of this table using sp_spaceused command. It is difficult to run manually each and evry database because it was taking long time.

I have created script (PFA) for this requiremnt, but script returns results for single database in server, but i need results of all databases in server (250 DB's). Please let me know how to modify attached script to execute against all databases in server.

The table is exist in all 250 DB's.
3.txt
0
Comment
Question by:sg05121983
  • 3
  • 2
6 Comments
 
LVL 8

Accepted Solution

by:
venk_r earned 250 total points
ID: 36537502
Try the below script
and If you want to ignore any database other than system database please include it in the where clause



CREATE TABLE #TableInfo (Name sysname NULL,
                         Rows int,
                         Reserved varchar(256) NULL,
                         Data varchar(256) NULL,
                         Index_Size varchar(256) NULL,
                         Unused varchar(256) NULL)
CREATE TABLE #DBTables (Instance sysname NULL,
                        DBName sysname NULL,
                        TableName sysname NULL,
                        --TableType char(2),
                        TableRows int NULL,
                        --IndexCount int NULL,
                        ReservedKB int NULL,
                        DataSizeKB int NULL,
                        IndexSizeKB int NULL,
                        UnusedKB int NULL)

DECLARE @DatabaseName varchar(64)
DECLARE @TableName varchar(256)
DECLARE @xtype char(2)
DECLARE @TableRows int
DECLARE @IndexCount int
DECLARE @ReservedKB int
DECLARE @DataSizeKB int
DECLARE @IndexSizeKB int
DECLARE @UnusedKB int
DECLARE @Table varchar(50)

DECLARE @DB varchar(50)
DECLARE @ExecSQL VARCHAR(2000)
SET @Table='Mytable'
DECLARE cs CURSOR FOR
  SELECT     NAME FROM MASTER..SYSDATABASES WHERE DBID>5 --Ignore system database

OPEN cs
FETCH NEXT FROM cs INTO @DB
WHILE (@@FETCH_STATUS = 0)
BEGIN
  TRUNCATE TABLE #TableInfo
  SET @ExecSQL = 'INSERT #TableInfo EXEC ' + @DB +'.dbo.sp_spaceused ' + @Table + ',@updateusage = ''TRUE'''
  PRINT @ExecSQL
 -- EXEC @ExecSQL



  SELECT
    @TableRows = Rows,
    @ReservedKB = CAST(SUBSTRING(Reserved, 1, CHARINDEX('KB', Reserved, 1)-1) AS int),
    @DataSizeKB = CAST(SUBSTRING(Data, 1, CHARINDEX('KB', Data, 1)-1) AS int),
    @IndexSizeKB = CAST(SUBSTRING(Index_Size, 1, CHARINDEX('KB', Index_Size, 1)-1) AS int),
    @UnusedKB = CAST(SUBSTRING(Unused, 1, CHARINDEX('KB', Unused, 1)-1) AS int)
  FROM #TableInfo


  INSERT INTO #DBTables
  VALUES (@@SERVERNAME,
          @DB,
          @Table,
          --@xtype,
          @TableRows,
          --@IndexCount,
          @ReservedKB,
          @DataSizeKB,
          @IndexSizeKB,
          @UnusedKB)

  FETCH NEXT FROM cs INTO @DB
END
CLOSE cs
DEALLOCATE cs

SELECT
  Instance,
  DBName,
  TableName,
  --TableType,
  TableRows,
  --IndexCount,
  ReservedKB,
  DataSizeKB,
  IndexSizeKB,
  UnusedKB
FROM #DBTables

DROP TABLE #DBTables
DROP TABLE #TableInfo
0
 

Author Comment

by:sg05121983
ID: 36540666
Hi Venk_r,

The Results are showing as NULL(TableRows, ReservedKB,DataSizeKB,IndexSizeKB,UnusedKB).

FYI...
In mytable (for X34 DB) : the number of rows are 111111
In mytable (for X35 DB) the number of rows are 222222.

But script is displaying results as NULL. Please review the script once again.

Thanks for your help.


0
 
LVL 10

Expert Comment

by:sqlservr
ID: 36541959
CREATE PROCEDURE GetAllTableSizes
AS
/*
    Obtains spaced used data for ALL user tables in the database
*/
DECLARE @TableName VARCHAR(100)    --For storing values in the cursor

--Cursor to get the name of all user tables from the sysobjects listing
DECLARE tableCursor CURSOR
FOR
select [name]
from dbo.sysobjects
where  OBJECTPROPERTY(id, N'IsUserTable') = 1
FOR READ ONLY

--A procedure level temp table to store the results
CREATE TABLE #TempTable
(
    tableName varchar(100),
    numberofRows varchar(100),
    reservedSize varchar(50),
    dataSize varchar(50),
    indexSize varchar(50),
    unusedSize varchar(50)
)

--Open the cursor
OPEN tableCursor

--Get the first table name from the cursor
FETCH NEXT FROM tableCursor INTO @TableName

--Loop until the cursor was not able to fetch
WHILE (@@Fetch_Status >= 0)
BEGIN
    --Dump the results of the sp_spaceused query to the temp table
    INSERT  #TempTable
        EXEC sp_spaceused @TableName

    --Get the next table name
    FETCH NEXT FROM tableCursor INTO @TableName
END

--Get rid of the cursor
CLOSE tableCursor
DEALLOCATE tableCursor

--Select all records so we can use the reults
SELECT *
FROM #TempTable

--Final cleanup!
DROP TABLE #TempTable

GO


open management studio and type EXEC GetAllTableSizes in new query windows
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:sg05121983
ID: 36542034
Mr. Sqlserver,

Have you understood, my reuirment?
0
 
LVL 8

Expert Comment

by:venk_r
ID: 36542629
Sorry I commented out certain statements for testing.
Here you go
CREATE TABLE #TableInfo (Name sysname NULL,
                         Rows int,
                         Reserved varchar(256) NULL,
                         Data varchar(256) NULL,
                         Index_Size varchar(256) NULL,
                         Unused varchar(256) NULL)
CREATE TABLE #DBTables (Instance sysname NULL,
                        DBName sysname NULL,
                        TableName sysname NULL,
                        --TableType char(2),
                        TableRows int NULL,
                        --IndexCount int NULL,
                        ReservedKB int NULL,
                        DataSizeKB int NULL,
                        IndexSizeKB int NULL,
                        UnusedKB int NULL)

DECLARE @DatabaseName varchar(64)
DECLARE @TableName varchar(256)
DECLARE @xtype char(2)
DECLARE @TableRows int
DECLARE @IndexCount int
DECLARE @ReservedKB int
DECLARE @DataSizeKB int
DECLARE @IndexSizeKB int
DECLARE @UnusedKB int
DECLARE @Table varchar(50)

DECLARE @DB varchar(50)
DECLARE @ExecSQL VARCHAR(4000)
SET @Table='MyTable'
DECLARE cs CURSOR FOR
  SELECT     NAME FROM MASTER..SYSDATABASES WHERE DBID>5 --If you want to ignore any database other than system database please include it in the where clause

OPEN cs
FETCH NEXT FROM cs INTO @DB
WHILE (@@FETCH_STATUS = 0)
BEGIN
  TRUNCATE TABLE #TableInfo
  SET @ExecSQL = 'INSERT into tempdb..#TableInfo EXEC ' + @DB +'.dbo.sp_spaceused ' + @Table + ',@updateusage = ''TRUE'''
 select  @ExecSQL
  EXECute( @ExecSQL)



  SELECT
    @TableRows = Rows,
    @ReservedKB = CAST(SUBSTRING(Reserved, 1, CHARINDEX('KB', Reserved, 1)-1) AS int),
    @DataSizeKB = CAST(SUBSTRING(Data, 1, CHARINDEX('KB', Data, 1)-1) AS int),
    @IndexSizeKB = CAST(SUBSTRING(Index_Size, 1, CHARINDEX('KB', Index_Size, 1)-1) AS int),
    @UnusedKB = CAST(SUBSTRING(Unused, 1, CHARINDEX('KB', Unused, 1)-1) AS int)
  FROM #TableInfo


  INSERT INTO #DBTables
  VALUES (@@SERVERNAME,
          @DB,
          @Table,
          --@xtype,
          @TableRows,
          --@IndexCount,
          @ReservedKB,
          @DataSizeKB,
          @IndexSizeKB,
          @UnusedKB)

  FETCH NEXT FROM cs INTO @DB
END
CLOSE cs
DEALLOCATE cs
SELECT
  Instance,
  DBName,
  TableName,
  --TableType,
  TableRows,
  --IndexCount,
  ReservedKB,
  DataSizeKB,
  IndexSizeKB,
  UnusedKB
FROM #DBTables

DROP TABLE #DBTables
DROP TABLE #TableInfo

0
 

Author Closing Comment

by:sg05121983
ID: 36547178
--
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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