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.
Author |
Topic |
luga
Starting Member
1 Post |
Posted - 2008-04-03 : 13:33:12
|
Hello,I need to put in a table the allocated space, used space and free space for all datafiles and transaction logs all databases in a specific server.The code is :EXEC tempdb.dbo.spSpaceUsedCREATE PROCEDURE spSpaceUsed ASCREATE TABLE tempdb.dbo.DBAloc( rowid int identity (1,1), DbId Int, DbName varchar (100), Name varchar (100), FileName varchar (200), Alocated float, FreeSpace float, SpaceUsed float,)CREATE TABLE tempdb.dbo.Databases( rowid int identity (1,1), dbid int, dbname varchar(100) )INSERT INTO Databases (dbid, dbname)select dbid, sd.namefrom master.dbo.sysdatabases sd--declare @dbCount intdeclare @dbName varchar(100)declare @ssql varchar (100)select @dbCount = @@rowcount--count down all the rowids to loop on all databasesWHILE @dbCount > 0 BEGIN SELECT @DBName = ltrim(rtrim([Dbname])) FROM Databases WHERE rowid = @DbCount SET @ssql = 'USE ' + @DBName-- doen't seem to work : EXEC(@ssql) INSERT INTO dbAloc ( Name , FileName , Alocated , FreeSpace , SpaceUsed ) select name, filename, size/128.0 as alocated, (size - fileproperty (name, 'SpaceUsed'))/128.0 as freespace, fileproperty (name, 'SpaceUsed')/128.0 as spaceused from northwind..sysfiles SET @dbcount = @dbcount - 1ENDGOThe problem I have with this code is the context of fileproperty is allways the current database it doen't seems to respect the use statement.How to accomplish my objective ?All the best,Luga. |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-04-03 : 13:41:11
|
To monitor Disk spaces used by database for Trend-Analysis,Here you go:http://www.sqlservercentral.com/articles/Administering/monitoringdiskspaceandsendingalertswithtsql/1114/ |
 |
|
|
|
|
|
|