Solved

Query List of Databases and then loop through to get information

Posted on 2012-04-04
4
408 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 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

742 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