Technical Article

Monitor database size

,

Display disk space, unallocated space in every your database and write resutl in table USED_DISK for analisys

create procedure dbo.sp_spaceused2
@dbname sysname

as
create table #spt_space
(
rowsint null,
reserveddec(15) null,
datadec(15) null,
indexpdec(15) null,
unuseddec(15) null
)


select @dbname = isnull(@dbname, db_name())

set nocount on
exec("

declare @idint-- The object id of @objname.
declare @typecharacter(2) -- The object type.
declare@pagesint-- Working variable for size calc.

declare @dbsize dec(15,0)
declare @logsize dec(15)
declare @bytesperpagedec(15,0)
declare @pagesperMBdec(15,0)

select @dbsize = sum(convert(dec(15),size))
from " + @dbname + ".dbo.sysfiles
where (status & 64 = 0)

select @logsize = sum(convert(dec(15),size))
from " + @dbname + ".dbo.sysfiles
where (status & 64 <> 0)

select @bytesperpage = low
from master.dbo.spt_values
where number = 1
and type = 'E'
select @pagesperMB = 1048576 / @bytesperpage

select  database_name = '" + @dbname + "',
database_size =
ltrim(str((@dbsize + @logsize) / @pagesperMB,15,2) + ' MB'),
'unallocated space' =
ltrim(str((@dbsize -
(select sum(convert(dec(15),reserved))
from " + @dbname + ".dbo.sysindexes
where indid in (0, 1, 255)
)) / @pagesperMB,15,2)+ ' MB'),
'date' = GETDATE()

")
return


GO
CREATE TABLE USED_DISK (
UsID Integer not null Identity,
Dbname varchar(20),
SizeDb varchar(20),
UnlockDB varchar(20),
Data datetime,
Primary Key(UsID)
)
GO
CREATE PROCEDURE Analys_base as
BEGIN
 declare @dbname varchar(30)
 declare base_cursor CURSOR local static FOR
 select name from master..sysdatabases
 OPEN base_cursor
 FETCH NEXT FROM base_cursor INTO @dbname
 WHILE @@FETCH_STATUS = 0
  BEGIN
   insert into USED_DISK
   exec master..sp_spaceused2 @dbname
   FETCH NEXT FROM base_cursor INTO @dbname
  END
 CLOSE base_cursor
 DEALLOCATE base_cursor
END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating