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 |
TinyJimbo
Starting Member
3 Posts |
Posted - 2010-08-06 : 15:03:16
|
I am new to T-SQL and I need help with an operation. It's all about getting the size of datafiles and logfiles for each databases.select 'SELECT ''' + name + ''' AS DBNAME,' +'sum(size * 8 /1024) AS MB from ' + name + '.dbo.sysfiles union 'from sysdatabasesorder by name(This query gives me queries for each databases in the instance)Result :SELECT 'master' AS DBNAME,sum(size * 8 /1024) AS MB from master.dbo.sysfiles unionSELECT 'model' AS DBNAME,sum(size * 8 /1024) AS MB from model.dbo.sysfiles unionSELECT 'msdb' AS DBNAME,sum(size * 8 /1024) AS MB from msdb.dbo.sysfiles unionSELECT 'Northwind' AS DBNAME,sum(size * 8 /1024) AS MB from Northwind.dbo.sysfiles unionSELECT 'pubs' AS DBNAME,sum(size * 8 /1024) AS MB from pubs.dbo.sysfiles unionSELECT 'tempdb' AS DBNAME,sum(size * 8 /1024) AS MB from tempdb.dbo.sysfilesThen I have to launch another query with these to get my results.What I'm trying to do it skip that middle part and have it execute the queries by itself.The query below works well but I don't like how the results are displayed.EXEC sp_MSforeachdb 'USE ? SELECT name,filename AS Path,(size*8)/1024 AS MBFROM dbo.sysfiles' |
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2010-08-06 : 16:08:12
|
An alternative is to use SMO and powershell - which would allow you instantiate such as and allow you format as you please. It depends how you want to manage \ and report on servers :$instance = 'MYSERVER\INSTANCE1'$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance$dbs = $s.Databasesforeach ($db in $dbs){ $db.size}Jack Vamvas--------------------http://www.sqlserver-dba.com |
 |
|
|
|
|