MOnitoring table growth size

  • Hi

    Can anyone please provide with the script to monitor all the tables in all databases on a particular server.

    Or please let me know how to proceed for this.

    Thanks

  • Try using the below mentioned script in a stored procedure and loop it through all databases on the server. Create and schedule a sql agent job which runs this stored procedure and export its output in a centralised table. Then you can poll this sentral table to track the growth.

    SET NOCOUNT ON

    IF EXISTS(SELECT * FROM TempDb.dbo.SysObjects WHERE NAME = '##Space_Used') DROP TABLE ##Space_Used

    CREATE TABLE ##Space_Used (

    name nvarchar(128),

    rows char(11),

    reserved varchar(18),

    data varchar(18),

    index_size varchar(18),

    unused varchar(18)

    )

    DECLARE @User_Table_Name varchar(200)

    DECLARE User_Tables_Cursor CURSOR FOR

    SELECT Name

    FROM Dbo.SysObjects

    WHERE XTYPE = 'U'

    OPEN User_Tables_Cursor

    FETCH NEXT FROM User_Tables_Cursor INTO @User_Table_Name

    WHILE @@FETCH_STATUS = 0

    BEGIN

    INSERT INTO ##Space_Used

    EXEC sp_spaceused @User_Table_Name

    FETCH NEXT FROM User_Tables_Cursor INTO @User_Table_Name

    END

    CLOSE User_Tables_Cursor

    DEALLOCATE User_Tables_Cursor

    IF EXISTS(SELECT * FROM TempDb.dbo.SysObjects WHERE NAME = '##Space_Used2') DROP TABLE ##Space_Used2

    SELECT

    TableName=LEFT(Name, 50),

    "RowCount"=Rows,

    Reserved_Phys_Size_KB = CONVERT(int, LEFT(Reserved, PATINDEX('% KB', Reserved) - 1))

    FROM ##Space_Used

    ORDER BY Reserved_Phys_Size_KB DESC

    IF EXISTS(SELECT * FROM TempDb.dbo.SysObjects WHERE NAME = '##Space_Used') DROP TABLE ##Space_Used

    HTH,

    MJ

  • Have you checked out the scripts section on the site? I'm pretty certain the one I've posted here was taken from there:

    /*********************************************************

    ** Database Object Sizing

    *********************************************************/

    USE [/*YOUR DATABASE NAME HERE*/]

    SELECT SCHEMA_NAME(TBL.SCHEMA_ID) AS [SCHEMA]

    , TBL.NAME

    , COALESCE((SELECT PR.NAME

    FROM SYS.DATABASE_PRINCIPALS PR

    WHERE PR.PRINCIPAL_ID = TBL.PRINCIPAL_ID)

    , SCHEMA_NAME(TBL.SCHEMA_ID)) AS [OWNER]

    , TBL.MAX_COLUMN_ID_USED AS [COLUMNS]

    , CAST(CASE IDX.INDEX_ID WHEN 1 THEN 1 ELSE 0 END AS BIT) AS [HASCLUSIDX]

    , COALESCE( ( SELECT SUM (SPART.ROWS) FROM SYS.PARTITIONS SPART

    WHERE SPART.OBJECT_ID = TBL.OBJECT_ID AND SPART.INDEX_ID < 2), 0) AS [ROWCOUNT]

    , COALESCE( (SELECT CAST(V.LOW/1024.0 AS FLOAT)

    * SUM(A.USED_PAGES - CASE WHEN A.TYPE <> 1 THEN A.USED_PAGES WHEN P.INDEX_ID < 2 THEN A.DATA_PAGES ELSE 0 END)

    FROM SYS.INDEXES AS I

    JOIN SYS.PARTITIONS AS P ON P.OBJECT_ID = I.OBJECT_ID AND P.INDEX_ID = I.INDEX_ID

    JOIN SYS.ALLOCATION_UNITS AS A ON A.CONTAINER_ID = P.PARTITION_ID

    WHERE I.OBJECT_ID = TBL.OBJECT_ID)

    , 0.0) AS [INDEXKB]

    , COALESCE( (SELECT CAST(V.LOW/1024.0 AS FLOAT)

    * SUM(CASE WHEN A.TYPE <> 1 THEN A.USED_PAGES WHEN P.INDEX_ID < 2 THEN A.DATA_PAGES ELSE 0 END)

    FROM SYS.INDEXES AS I

    JOIN SYS.PARTITIONS AS P ON P.OBJECT_ID = I.OBJECT_ID AND P.INDEX_ID = I.INDEX_ID

    JOIN SYS.ALLOCATION_UNITS AS A ON A.CONTAINER_ID = P.PARTITION_ID

    WHERE I.OBJECT_ID = TBL.OBJECT_ID)

    , 0.0) AS [DATAKB]

    FROM SYS.TABLES AS TBL

    INNER JOIN SYS.INDEXES AS IDX ON (IDX.OBJECT_ID = TBL.OBJECT_ID AND IDX.INDEX_ID < 2)

    INNER JOIN MASTER.DBO.SPT_VALUES V ON (V.NUMBER=1 AND V.TYPE='E')

    ORDER BY 1,6 DESC

  • Try this:

    DECLARE @sTableName SYSNAME

    DECLARE @sSQL NVARCHAR(max)

    DECLARE @iRowCount INT

    DECLARE @t_TableNames_Temp TABLE

    (table_name SYSNAME)

    Drop Table TableGrowth

    Create Table TableGrowth

    (

    DBName varchar(32),

    SchemaName varchar(32),

    TableName varchar(50),

    TableID int,

    MinRowSize int,

    MaxRowSize int,

    ReservedKB int,

    DataKB int,

    IndexKB int,

    UnusedKB int,

    Rows int,

    RowModCtr int,

    HasTextImage int,

    HasClustered int

    )

    INSERT @t_TableNames_Temp

    SELECT name

    FROM SYS.DATABASES

    WHERE name not in ('master','msdb','model','tempdb')

    ORDER BY name

    --Getting row count from table

    SELECT @iRowCount = COUNT(*) FROM @t_TableNames_Temp

    WHILE @iRowCount > 0

    BEGIN

    SELECT @sTableName = table_name from @t_TableNames_Temp

    SELECT @sSQL = 'SELECT DBName = '''+@sTableName+''',

    --SchemaName = SCHEMA_NAME(so.UID), --Comment out if for SQL Server 2000

    SchemaName = USER_NAME(so.UID), --Comment out if for SQL Server 2005

    TableName = so.Name,

    TableID = so.ID,

    MinRowSize = MIN(si.MinLen),

    MaxRowSize = MAX(si.XMaxLen),

    ReservedKB = SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Reserved ELSE 0 END) * pkb.PageKB,

    DataKB = SUM(CASE WHEN si.IndID IN (0,1) THEN si.DPages ELSE 0 END) * pkb.PageKB

    + SUM(CASE WHEN si.IndID IN ( 255) THEN ISNULL(si.Used,0) ELSE 0 END) * pkb.PageKB,

    IndexKB = SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Used ELSE 0 END) * pkb.PageKB

    - SUM(CASE WHEN si.IndID IN (0,1) THEN si.DPages ELSE 0 END) * pkb.PageKB

    - SUM(CASE WHEN si.IndID IN ( 255) THEN ISNULL(si.Used,0) ELSE 0 END) * pkb.PageKB,

    UnusedKB = SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Reserved ELSE 0 END) * pkb.PageKB

    - SUM(CASE WHEN si.IndID IN (0,1,255) THEN si.Used ELSE 0 END) * pkb.PageKB,

    Rows = SUM(CASE WHEN si.IndID IN (0,1) THEN si.Rows ELSE 0 END),

    RowModCtr = MIN(si.RowModCtr),

    HasTextImage = MAX(CASE WHEN si.IndID IN ( 255) THEN 1 ELSE 0 END),

    HasClustered = MAX(CASE WHEN si.IndID IN ( 1) THEN 1 ELSE 0 END)

    FROM '+@sTableName+'.dbo.SysObjects so, '

    +@sTableName+'.dbo.SysIndexes si,

    (--Derived table finds page size in KB according to system type

    SELECT Low/1024 AS PageKB --1024 is a binary Kilo-byte

    FROM Master.dbo.spt_Values

    WHERE Number = 1 --Identifies the primary row for the given type

    AND Type = ''E'' --Identifies row for system type

    )pkb

    WHERE si.ID = so.ID

    AND si.IndID IN (0, --Table w/o Text or Image Data

    1,

    255) --Table w/ Text or Image Data

    AND so.XType = ''U'' --User Tables

    --AND PERMISSIONS(so.ID) <> 0

    GROUP BY so.Name,

    so.UID,

    so.ID,

    pkb.PageKB

    ORDER BY ReservedKB DESC'

    PRINT @SSQL

    Insert TableGrowth

    EXEC (@sSQL)

    DELETE FROM @t_TableNames_Temp WHERE @sTableName = table_name

    SELECT @iRowCount = @iRowCount - 1

    END

    Select * from TableGrowth

    SET NOCOUNT OFF

    GO

    HTH,

    MJ

  • nikhil.verma (1/7/2009)


    Hi

    Can anyone please provide with the script to monitor all the tables in all databases on a particular server.

    Or please let me know how to proceed for this.

    Thanks

    ----------------------Row count and space used for all tables in database------------------------

    /*****************************************************************************************************************************************

    *

    * Purpose This procedure returns a recorset with row count and space used for all tables

    * in the specified database (or for all databases except tempdb and model)

    * Date 2008.03.05 (version for SQL Server 2005)

    *

    ******************************************************************************************************************************************/

    if exists(select * from sys.objects where object_id = object_id('dbo.proc_records_per_database') and type = 'P')

    drop procedure dbo.proc_records_per_database

    go

    create procedure dbo.proc_records_per_database

    @db_name sysname = NULL

    as

    begin

    set nocount on

    if @db_name is not null

    begin

    if not exists(select * from master.sys.databases where name = @db_name and database_id not in (2, 3) /* skip tempdb and model */)

    begin

    raiserror('Database does not exist or can not be queried', 16, 1)

    return

    end

    end

    declare @dbs_table table(name sysname not null primary key)

    --------------------------------------------------------------------------------------

    -- prepare databases list

    insert into @dbs_table

    select

    name

    from

    master.sys.databases

    where

    (@db_name is null or

    (@db_name is not null and name = @db_name)) and

    database_id not in (2, 3)

    create table #temp_table_list

    (

    rec_id int identity(1, 1) not null,

    cat_name sysname not null,

    sch_name sysname not null,

    tab_name sysname not null,

    row_count bigint not null default 0,

    reserved_pages bigint not null default 0,

    reserved bigint not null default 0,

    pages bigint not null default 0,

    data bigint not null default 0,

    used_pages bigint not null default 0,

    used bigint not null default 0,

    index_size bigint not null default 0,

    not_used bigint not null default 0,

    primary key(rec_id)

    )

    declare @cmd varchar(max)

    declare @n_cmd nvarchar(max)

    declare @dbx_name sysname

    --------------------------------------------------------------------------------

    -- get all tables

    while 1 = 1

    begin

    set @dbx_name = NULL

    select top 1 @dbx_name = [name] from @dbs_table

    if @dbx_name is NULL

    break

    set @cmd = 'insert into #temp_table_list (cat_name, sch_name, tab_name) select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME from [' + @dbx_name + '].INFORMATION_SCHEMA.TABLES where TABLE_TYPE = ''BASE TABLE'''

    set @n_cmd = cast(@cmd as nvarchar(max))

    exec sp_executesql @n_cmd

    delete from @dbs_table where [name] = @dbx_name

    end

    declare @max_rec_id int

    set @max_rec_id = NULL

    select @max_rec_id = max(rec_id) from #temp_table_list

    if @max_rec_id is NULL

    set @max_rec_id = -1

    declare @counter int

    set @counter = 1

    declare @objname varchar(max)

    while @counter <= @max_rec_id

    begin

    --------------------------------------------------------------------------------

    -- update table usage statistics

    select

    @cmd = 'use [' + cat_name + ']; dbcc updateusage(0, ''[' + sch_name + '].[' + tab_name + ']'') with no_infomsgs'

    from

    #temp_table_list

    where

    rec_id = @counter

    set @n_cmd = cast(@cmd as nvarchar(max))

    -- print @n_cmd

    exec sp_executesql @n_cmd

    -----------------------------------------------------------------------------------------------

    -- get table stats (based on the code of the procedure sp_spaceused)

    declare @reservedpages_param bigint

    declare @usedpages_param bigint

    declare @pages_param bigint

    declare @index_size_param bigint

    declare @unused_param bigint

    declare @rows_param bigint

    set @reservedpages_param = 0

    set @usedpages_param = 0

    set @pages_param = 0

    set @index_size_param = 0

    set @unused_param = 0

    set @rows_param = 0

    select

    @cmd =

    ' use [' + cat_name + '];

    declare @id int

    select @id = object_id(''[' + sch_name + '].[' + tab_name + ']'')

    SELECT

    @reservedpages = sum(reserved_page_count),

    @usedpages = sum(used_page_count),

    @pages = sum(

    CASE

    WHEN (index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)

    ELSE lob_used_page_count + row_overflow_used_page_count

    END

    ),

    @rowCount = sum(

    CASE

    WHEN (index_id < 2) THEN row_count

    ELSE 0

    END

    )

    FROM sys.dm_db_partition_stats

    WHERE object_id = @id;

    IF (SELECT count(*) FROM sys.internal_tables WHERE parent_id = @id AND internal_type IN (202,204)) > 0

    BEGIN

    SELECT

    @reservedpages = @reservedpages + sum(reserved_page_count),

    @usedpages = @usedpages + sum(used_page_count)

    FROM sys.dm_db_partition_stats p, sys.internal_tables it

    WHERE it.parent_id = @id AND it.internal_type IN (202,204) AND p.object_id = it.object_id;

    END

    SET @reservedpages = @reservedpages

    SET @index_size = (CASE WHEN @usedpages > @pages THEN (@usedpages - @pages) ELSE 0 END) * 8

    SET @unused = (CASE WHEN @reservedpages > @usedpages THEN (@reservedpages - @usedpages) ELSE 0 END) * 8'

    from

    #temp_table_list

    where

    rec_id = @counter

    set @n_cmd = cast(@cmd as nvarchar(max))

    exec sp_executesql

    @n_cmd,

    @parameters = N'@reservedpages bigint OUTPUT, @usedpages bigint OUTPUT, @pages bigint OUTPUT, @index_size bigint OUTPUT, @unused bigint OUTPUT, @rowCount bigint OUTPUT',

    @reservedpages = @reservedpages_param OUTPUT,

    @usedpages = @usedpages_param OUTPUT,

    @pages = @pages_param OUTPUT,

    @index_size = @index_size_param OUTPUT,

    @unused = @unused_param OUTPUT,

    @rowCount = @rows_param OUTPUT

    update

    #temp_table_list

    set

    row_count = @rows_param,

    reserved_pages = @reservedpages_param,

    reserved = @reservedpages_param * 8,

    data = @pages_param * 8,

    index_size = @index_size_param,

    not_used = @unused_param,

    pages = @pages_param,

    used_pages = @usedpages_param,

    used = @usedpages_param * 8

    where

    rec_id = @counter

    set @counter = @counter + 1

    end

    select

    cat_name,

    sch_name,

    tab_name,

    row_count,

    reserved_pages,

    used_pages,

    pages,

    reserved,

    used,

    data,

    index_size,

    not_used

    from

    #temp_table_list

    order by

    cat_name,

    sch_name,

    tab_name

    drop table #temp_table_list

    end

    go

    -- example A:

    exec dbo.proc_records_per_database 'AdventureWorks'

    -- example A:

    exec dbo.proc_records_per_database

    /*****************************************************************************************************************************************

    *

    * Author Rafal Skotak

    * Purpose This procedure returns a recorset with row count and space used for all tables

    * in the specified database (or for all databases except tempdb and model)

    * This is the version for SQL Server 2000

    * Date 2008.04.21

    *

    ******************************************************************************************************************************************/

    if exists(select * from sysobjects where id = object_id('dbo.proc_records_per_database') and type = 'P')

    drop procedure dbo.proc_records_per_database

    go

    create procedure dbo.proc_records_per_database

    @db_name sysname = NULL

    as

    begin

    set nocount on

    if @db_name is not null

    begin

    if not exists(select * from master.dbo.sysdatabases where name = @db_name and dbid not in (2, 3) /* skip tempdb and model */)

    begin

    raiserror('Database does not exist or can not be queried', 16, 1)

    return

    end

    end

    declare @dbs_table table(name sysname not null primary key)

    --------------------------------------------------------------------------------------

    -- prepare databases list

    insert into @dbs_table

    select

    name

    from

    master.dbo.sysdatabases

    where

    (@db_name is null or

    (@db_name is not null and name = @db_name)) and

    dbid not in (2, 3)

    create table #temp_table_list

    (

    rec_id int identity(1, 1) not null,

    cat_name sysname not null,

    sch_name sysname not null,

    tab_name sysname not null,

    row_count bigint not null default 0,

    reserved_pages bigint not null default 0,

    reserved bigint not null default 0,

    pages bigint not null default 0,

    data bigint not null default 0,

    used_pages bigint not null default 0,????

    used bigint not null default 0,

    index_size bigint not null default 0,

    not_used bigint not null default 0,

    primary key(rec_id)

    )

    declare @cmd varchar(4000)

    declare @n_cmd nvarchar(4000)

    declare @dbx_name sysname

    --------------------------------------------------------------------------------

    -- get all tables

    while 1 = 1

    begin

    set @dbx_name = NULL

    select top 1 @dbx_name = [name] from @dbs_table

    if @dbx_name is NULL

    break

    set @cmd = 'insert into #temp_table_list (cat_name, sch_name, tab_name) select ''' + @dbx_name + ''', '''', name from [' + @dbx_name + '].dbo.sysobjects where type = ''U'''

    set @n_cmd = cast(@cmd as nvarchar(4000))

    exec sp_executesql @n_cmd

    delete from @dbs_table where [name] = @dbx_name

    end

    declare @max_rec_id int

    set @max_rec_id = NULL

    select @max_rec_id = max(rec_id) from #temp_table_list

    if @max_rec_id is NULL

    set @max_rec_id = -1

    declare @counter int

    set @counter = 1

    declare @objname varchar(4000)

    while @counter <= @max_rec_id

    begin

    --------------------------------------------------------------------------------

    -- update table usage statistics

    select

    @cmd = 'use [' + cat_name + ']; dbcc updateusage(0, ''[' + tab_name + ']'') with no_infomsgs'

    from

    #temp_table_list

    where

    rec_id = @counter

    set @n_cmd = cast(@cmd as nvarchar(4000))

    -- print @n_cmd

    exec sp_executesql @n_cmd

    -----------------------------------------------------------------------------------------------

    -- get table stats (based on the code of the procedure sp_spaceused)

    declare @reservedpages_param bigint

    declare @usedpages_param bigint

    declare @pages_param bigint

    declare @index_size_param bigint

    declare @unused_param bigint

    declare @rows_param bigint

    set @reservedpages_param = 0

    set @usedpages_param = 0

    set @pages_param = 0

    set @index_size_param = 0

    set @unused_param = 0

    set @rows_param = 0

    select

    @cmd =

    'use [' + cat_name + '];

    declare @id int

    select @id = object_id(''[' + tab_name + ']'')

    select @reservedpages = sum(reserved) from sysindexes where indid in (0, 1, 255) and id = @id

    select @pages = sum(dpages) from sysindexes where indid < 2 and id = @id

    select @pages = @pages + isnull(sum(used), 0) from sysindexes where indid = 255 and id = @id

    select @index_size = (sum(used) - @pages) from sysindexes where indid in (0, 1, 255) and id = @id

    select @usedpages = @index_size + @pages

    set @index_size = @index_size * 8

    select @unused = (@reservedpages - (select sum(used) from sysindexes where indid in (0, 1, 255) and id = @id)) * 8

    select @rowCount = rows from sysindexes where indid < 2 and id = @id'

    from

    #temp_table_list

    where

    rec_id = @counter

    set @n_cmd = cast(@cmd as nvarchar(4000))

    -- print @n_cmd

    exec sp_executesql

    @n_cmd,

    @parameters = N'@reservedpages bigint OUTPUT, @usedpages bigint OUTPUT, @pages bigint OUTPUT, @index_size bigint OUTPUT, @unused bigint OUTPUT, @rowCount bigint OUTPUT',

    @reservedpages = @reservedpages_param OUTPUT,

    @usedpages = @usedpages_param OUTPUT,

    @pages = @pages_param OUTPUT,

    @index_size = @index_size_param OUTPUT,

    @unused = @unused_param OUTPUT,

    @rowCount = @rows_param OUTPUT

    update

    #temp_table_list

    set

    row_count = @rows_param,

    reserved_pages = @reservedpages_param,

    reserved = @reservedpages_param * 8,

    data = @pages_param * 8,

    index_size = @index_size_param,

    not_used = @unused_param,

    pages = @pages_param,

    used_pages = @usedpages_param,

    used = @usedpages_param * 8

    where

    rec_id = @counter

    set @counter = @counter + 1

    end

    select

    cat_name,

    tab_name,

    row_count,

    reserved_pages,

    used_pages,

    pages as data_pages,

    reserved,

    used,

    data,

    index_size,

    not_used

    from

    #temp_table_list

    order by

    cat_name,

    tab_name

    drop table #temp_table_list

    end

    go

    -- example:

    exec dbo.proc_records_per_database 'AdventureWorks2000'

    exec dbo.proc_records_per_database 'Northwind'

    ---------------------------------------------------------------

    ----------------------------------------------------------------------------------

    --Easy way to track database growth-------------------

    select

    BackupDate = convert(varchar(10),backup_start_date, 111)

    ,SizeInGigs=floor( backup_size/1024000000)

    from msdb..backupset

    where

    database_name = 'DatabaseName'

    and type = 'd'

    order by

    backup_start_date desc

    ---------------------------------------------------------------

    ----------------------------------------------------------------------------------

    -----monitor growth-

    --PART 1

    If exists (Select name from sys.objects where name = 'DBGrowthRate' and Type = 'U')

    Drop Table dbo.DBGrowthRate

    Create Table dbo.DBGrowthRate (DBGrowthID int identity(1,1), DBName varchar(100), DBID int,

    NumPages int, OrigSize decimal(10,2), CurSize decimal(10,2), GrowthAmt varchar(100),

    MetricDate datetime)

    Select sd.name as DBName, mf.name as FileName, mf.database_id, file_id, size

    into #TempDBSize

    from sys.databases sd

    join sys.master_files mf

    on sd.database_ID = mf.database_ID

    Order by mf.database_id, sd.name

    Insert into dbo.DBGrowthRate (DBName, DBID, NumPages, OrigSize, CurSize, GrowthAmt, MetricDate)

    (Select tds.DBName, tds.database_ID, Sum(tds.Size) as NumPages,

    Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as OrigSize,

    Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as CurSize,

    '0.00 MB' as GrowthAmt, GetDate() as MetricDate

    from #TempDBSize tds

    where tds.database_ID not in (Select Distinct DBID from DBGrowthRate

    where DBName = tds.database_ID)

    Group by tds.database_ID, tds.DBName)

    Drop table #TempDBSize

    Select *

    from DBGrowthRate

    --Above creates initial table and checks initial data

    --PART 2

    --Below is the code run weekly to check the growth.

    Select sd.name as DBName, mf.name as FileName, mf.database_id, file_id, size

    into #TempDBSize2

    from sys.databases sd

    join sys.master_files mf

    on sd.database_ID = mf.database_ID

    Order by mf.database_id, sd.name

    If Exists (Select Distinct DBName from #TempDBSize2

    where DBName in (Select Distinct DBName from DBGrowthRate))

    and Convert(varchar(10),GetDate(),101) > (Select Distinct Convert(varchar(10),Max(MetricDate),101) as MetricDate

    from DBGrowthRate)

    Begin

    Insert into dbo.DBGrowthRate (DBName, DBID, NumPages, OrigSize, CurSize, GrowthAmt, MetricDate)

    (Select tds.DBName, tds.database_ID, Sum(tds.Size) as NumPages,

    dgr.CurSize as OrigSize,

    Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as CurSize,

    Convert(varchar(100),(Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024))

    - dgr.CurSize)) + ' MB' as GrowthAmt, GetDate() as MetricDate

    from #TempDBSize2 tds

    join DBGrowthRate dgr

    on tds.database_ID = dgr.DBID

    Where DBGrowthID = (Select Distinct Max(DBGrowthID) from DBGrowthRate

    where DBID = dgr.DBID)

    Group by tds.database_ID, tds.DBName, dgr.CurSize)

    End

    Else

    IF Not Exists (Select Distinct DBName from #TempDBSize2

    where DBName in (Select Distinct DBName from DBGrowthRate))

    Begin

    Insert into dbo.DBGrowthRate (DBName, DBID, NumPages, OrigSize, CurSize, GrowthAmt, MetricDate)

    (Select tds.DBName, tds.database_ID, Sum(tds.Size) as NumPages,

    Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as OrigSize,

    Convert(decimal(10,2),(((Sum(Convert(decimal(10,2),tds.Size)) * 8000)/1024)/1024)) as CurSize,

    '0.00 MB' as GrowthAmt, GetDate() as MetricDate

    from #TempDBSize2 tds

    where tds.database_ID not in (Select Distinct DBID from DBGrowthRate

    where DBName = tds.database_ID)

    Group by tds.database_ID, tds.DBName)

    End

    --Select *

    --from DBGrowthRate

    ----Verifies values were entered

    Drop table #TempDBSize2

    _____________________________________________________________________________________________________________
    Paresh Prajapati
    ➡ +919924626601
    http://paresh-sqldba.blogspot.com/[/url]
    LinkedIn | Tweet Me | FaceBook | Brijj

  • If you want to monitor it on multiple servers I would highly suggest going through this series of articles (here[/url]) and adding a step in there for this specific need. I've done similar requests.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply