Solved

Query List of Databases and then loop through to get information

Posted on 2012-04-04
4
404 Views
Last Modified: 2012-04-04
I need to get the 'name' from running the 'exec sp_helpdb' and then use it in a loop whcih would run the following command against it.

use Database1 << This is the 'name' from the sp_helpdb
select name , filename , convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB , convert(decimal(12,2),round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpaceUsedMB , convert(decimal(12,2),round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpaceMB from dbo.sysfiles a
go
use Database2 << This is the 'name' from the sp_helpdb
select name , filename , convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB , convert(decimal(12,2),round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpaceUsedMB , convert(decimal(12,2),round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpaceMB from dbo.sysfiles a
go
use Database3 << This is the 'name' from the sp_helpdb
select name , filename , convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB , convert(decimal(12,2),round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpaceUsedMB , convert(decimal(12,2),round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpaceMB from dbo.sysfiles a
go
....
0
Comment
Question by:edrz01
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
sachinpatil10d earned 500 total points
ID: 37805902
Try this

declare @dbName nvarchar(100)
declare @strSql nvarchar(1000)
declare @spdbdesc  table 
(  
 name sysname,  
 db_size nvarchar(100) null,  
 owner nvarchar(100),  
 dbid smallint,  
 created nvarchar(100),
 status nvarchar(600) null,  
 cmptlevel tinyint  
)  
insert into @spdbdesc
exec sp_helpdb
declare  cur cursor for
select name from @spdbdesc
open cur
fetch next from cur into @dbName
while @@FETCH_STATUS = 0
begin
	set @strSql = 'select name , filename , convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB , 
				convert(decimal(12,2),round(fileproperty(a.name,''SpaceUsed'')/128.000,2)) as SpaceUsedMB , 
				convert(decimal(12,2),round((a.size-fileproperty(a.name,''SpaceUsed''))/128.000,2)) as FreeSpaceMB 
				from ' + @dbName + '.dbo.sysfiles a'
				print @strSql
	exec sp_executesql @strSql
	fetch next from cur into @dbName
end
close cur
deallocate cur

Open in new window

0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37805908
you can even use  sys.databases  table to get all database names

declare @dbName nvarchar(100)
declare @strSql nvarchar(1000)
declare  cur cursor for
select name from sys.databases 
open cur
fetch next from cur into @dbName
while @@FETCH_STATUS = 0
begin
	set @strSql = 'select name , filename , convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB , 
				convert(decimal(12,2),round(fileproperty(a.name,''SpaceUsed'')/128.000,2)) as SpaceUsedMB , 
				convert(decimal(12,2),round((a.size-fileproperty(a.name,''SpaceUsed''))/128.000,2)) as FreeSpaceMB 
				from ' + @dbName + '.dbo.sysfiles a'
				print @strSql
	exec sp_executesql @strSql
	fetch next from cur into @dbName
end
close cur
deallocate cur

Open in new window

0
 

Author Closing Comment

by:edrz01
ID: 37805923
Thanks! The first solution work great. I tested the second one as well but feel that the first one is best suited.
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 37805947
welcome
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now