Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query List of Databases and then loop through to get information

Posted on 2012-04-04
4
Medium Priority
?
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
sachinpatil10d earned 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are some very powerful Dynamic Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a di…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

596 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