0

I am once again faced with an issue that my boss threw at me..

I created a small windows based application that connects to multiple MSSQL servers consecutively runs this query:

DECLARE @Orderby AS VARCHAR(50)
SET @Orderby = 'reserved_MB desc'
SET nocount ON
SET ansi_warnings OFF
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

CREATE TABLE #S
(
  [name] VARCHAR(50) NULL ,
  [rows] VARCHAR(50) NULL ,
  [reserved] VARCHAR(50) NULL ,
  [data] VARCHAR(50) NULL ,
  [index_size] VARCHAR(50) NULL ,
  [unused] VARCHAR(50) NULL
)

-- Create a cursor to loop through the user tables
DECLARE @name VARCHAR(50)
DECLARE c_tables CURSOR
FOR
    SELECT  name
    FROM    sysobjects
    WHERE   xtype = 'U'

OPEN c_tables
FETCH NEXT FROM c_tables
INTO @name

WHILE @@fetch_status = 0 
    BEGIN
    INSERT INTO #S
            EXEC sp_spaceUsed @name
    FETCH NEXT FROM c_tables    INTO @name
END

CLOSE c_tables
DEALLOCATE c_tables

SELECT  [dSampleDate] = GETDATE(),
    [name] ,
    [rows] ,
    reserved_MB ,
    data_MB ,
    [index_MB] ,
    unused_MB
INTO    #T
FROM    ( SELECT    [name] ,
                [rows] = CAST([rows] AS INT) ,
                reserved_MB = CAST(REPLACE(reserved, 'KB', '') AS INT)
                / 1000 ,
                data_MB = CAST(REPLACE(data, 'KB', '') AS INT) / 1000 ,
                [index_MB] = CAST(REPLACE(index_size, 'KB', '') AS INT)
                / 1000 ,
                unused_MB = CAST(REPLACE(unused, 'KB', '') AS INT) / 1000
      FROM      #S
    ) AS XX
ORDER BY reserved_MB DESC 

DROP TABLE #S
DROP TABLE #T

Which runs fine, it inserts it into a local database inside the reader with this command:

INSERT INTO [DBGrowth] (sName, sTableName, iRows, iReservedMB, iDataMB, iIndexMB, iUnusedMB) VALUES('" & sDbName & "', '" & RDR2.Item("name") & "', '" & RDR2.Item("rows") & "', '" & RDR2.Item("reserved_MB") & "', '" & RDR2.Item("data_MB") & "', '" & RDR2.Item("index_MB") & "', '" & RDR2.Item("unused_MB") & "')

I got all that to work just fine, not the following has me puzzled.. He wants that I generate daily reports on Rows increase, Data file increase and Index file increases. And as of yet I am unable to find a solution, the language I created the application in is in Visual Basic .NET and I am using MSSQL Server 2008.

Ideas and tips for either generating results in T-SQL or in Visual Studio (Application based) would be helpful.

If anyone can point me in the right direction that would be greatly appreciated.

Regards, Robert.

4

1 回答 1

0

您上面的 T-SQL 为您提供了一个很好的起点。您始终可以创建一个 SQL Server 代理,该代理每天运行上述 SQL 以填充 DBGrowth。这将为您提供您正在查看的各种项目的历史视图。无论如何,您都可以查询此 DBGrowth 表,以提供摘要信息。

于 2013-06-18T12:24:49.227 回答