Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Administration (2000)
 Administration Monitoring

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-11-13 : 08:52:57
Paul writes "Hello

I have seen allot of TSQL scripts which show how big the physical file is but I have been trying to find out a way that a script will report to you the size of the database within that operating system file and free space? I am quite sure that there must be a way as it shows it in the graphically form in EM but I have been unable to find a way.

I am running 2000 server SP3, sql 7 and 2000 both fully service packed.

Any help or pointers in the right direction would be greatly appreciated

thanks in advance


Paul"

efelito
Constraint Violating Yak Guru

478 Posts

Posted - 2002-11-13 : 11:48:14
The data from DBCC SHOWFILESTATS can be used to calculate the information you are looking for. This is what EM uses to display the graphical data. UsedExtents / 16 will give you MB used and (TotalExtents - UsedExtents) / 16 will give you MB free.

Jeff Banschbach
Consultant, MCDBA
Go to Top of Page

jasper_smith
SQL Server MVP & SQLTeam MVY

846 Posts

Posted - 2002-11-13 : 14:58:18


use master
go

CREATE procedure sp_dbspace
as
/* SQL7 and SQ2000 */
set nocount on

create table #results(FileType varchar(4) NOT NULL,
[Name] sysname NOT NULL,
Total numeric(9,2) NOT NULL,
Used numeric(9,2) NOT NULL,
[Free] numeric(9,2) NOT NULL)

create table #data(Fileid int NOT NULL,
[FileGroup] int NOT NULL,
TotalExtents int NOT NULL,
UsedExtents int NOT NULL,
[Name] sysname NOT NULL,
[FileName] varchar(300))

create table #log(DatabaseName sysname NOT NULL,
LogSize numeric(15,7) NOT NULL,
LogUsed numeric(9,5) NOT NULL,
Status int NOT NULL)

/* Get data file(s) size */
insert #data
exec('DBCC showfilestats')

/* Get log file(s) size */
insert #log
exec('dbcc sqlperf(logspace)')

insert #results(FileType,[Name],Total,Used,[Free])
select 'Data',
left(right([FileName],charindex('\',reverse([FileName]))-1),
charindex('.',right([FileName],
charindex('\',reverse([FileName]))-1))-1),
CAST(((TotalExtents*64)/1024.00) as numeric(9,2)),
CAST(((UsedExtents*64)/1024.00) as numeric(9,2)),
(CAST(((TotalExtents*64)/1024.00) as numeric(9,2))
-CAST(((UsedExtents*64)/1024.00) as numeric(9,2)))
from #data

union all

select 'Log',DatabaseName+'_log',LogSize,
((LogUsed/100)*LogSize),LogSize-((LogUsed/100)*LogSize)
from #log
where DatabaseName = db_name()

select * from #results

drop table #data
drop table #log
drop table #results
return


--improved below but sql2000 only due to sysaltfiles


use master
go

CREATE procedure sp_dbspaceall(@all char(1)='N')
as

set nocount on

declare @cmd varchar(500)
declare @db varchar(128)

create table #results(FileType varchar(4) NOT NULL,
[Name] sysname NOT NULL,
Total numeric(9,2) NOT NULL,
Used numeric(9,2) NOT NULL,
[Free] numeric(9,2) NOT NULL,
dbname sysname NULL)

create table #data(Fileid int NOT NULL,
[FileGroup] int NOT NULL,
TotalExtents int NOT NULL,
UsedExtents int NOT NULL,
[Name] sysname NOT NULL,
[FileName] varchar(300) NOT NULL)

create table #log(dbname sysname NOT NULL,
LogSize numeric(15,7) NOT NULL,
LogUsed numeric(9,5) NOT NULL,
Status int NOT NULL)

If @all='N' /* just the current database */
begin
/* Get data file(s) size */
insert #data
exec('DBCC showfilestats')

/* Get log file(s) size */
insert #log
exec('dbcc sqlperf(logspace)')

insert #results(FileType,[Name],Total,Used,[Free])
select 'Data',
left(right([FileName],charindex('\',reverse([FileName]))-1),
charindex('.',right([FileName],
charindex('\',reverse([FileName]))-1))-1),
CAST(((TotalExtents*64)/1024.00) as numeric(9,2)),
CAST(((UsedExtents*64)/1024.00) as numeric(9,2)),
(CAST(((TotalExtents*64)/1024.00) as numeric(9,2))
-CAST(((UsedExtents*64)/1024.00) as numeric(9,2)))
from #data

union all

select 'Log',dbname+'_log',LogSize,
((LogUsed/100)*LogSize),LogSize-((LogUsed/100)*LogSize)
from #log
where dbname = db_name()

select FileType,[Name],Total,Used,[Free]
from #results order by FileType

drop table #data
drop table #log
drop table #results

return
end
else
begin

/* Get data file(s) size */
declare dcur cursor local fast_forward
for
select CATALOG_NAME
from INFORMATION_SCHEMA.SCHEMATA

open dcur

fetch next from dcur into @db

while @@fetch_status=0
begin

set @cmd = 'use ' + @db + ' DBCC showfilestats'
insert #data
exec(@cmd)

insert #results(FileType,[Name],Total,Used,[Free],dbname)
select 'Data',
left(right([FileName],charindex('\',reverse([FileName]))-1),
charindex('.',right([FileName],
charindex('\',reverse([FileName]))-1))-1),
CAST(((TotalExtents*64)/1024.00) as numeric(9,2)),
CAST(((UsedExtents*64)/1024.00) as numeric(9,2)),
(CAST(((TotalExtents*64)/1024.00) as numeric(9,2))
-CAST(((UsedExtents*64)/1024.00) as numeric(9,2))),
@db
from #data

delete #data

fetch next from dcur into @db

end
close dcur
deallocate dcur

/* Get log file(s) size */
insert #log
exec('dbcc sqlperf(logspace)')

insert #results(FileType,[Name],Total,Used,[Free],dbname)
select 'Log',dbname+'_log',LogSize,
((LogUsed/100)*LogSize),LogSize-((LogUsed/100)*LogSize),
dbname
from #log


select dbname,FileType,[Name],Total,Used,[Free]
from #results order by dbname,FileType

drop table #data
drop table #log
drop table #results

return

end




HTH
Jasper Smith
Go to Top of Page
   

- Advertisement -