Solved

SQL Query database location and size

Posted on 2014-02-06
8
309 Views
Last Modified: 2014-02-10
So I can find a query to give me the name and location;

SELECT name, physical_name AS current_file_location
FROM sys.master_files

But I want to add a column that gives me the size as well.

How do I do that, thanks? :)
0
Comment
Question by:liminal
  • 4
  • 3
8 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 39840871
There is a column called size in sys.master_files that should give you that.

See: http://technet.microsoft.com/en-us/library/ms186782.aspx

To transform from 8K pages to MB or GB:

         SizeInMB = (size * 8 / 1024)
         SizeInGB = (size * 8 / 1024 / 1024)
0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 39840880
SELECT
      mf.name,
      mf.physical_name,
      df.size
FROM
      sys.master_files mf INNER JOIN sys.database_files df
        ON mf.[name] = df.[name]
0
 

Author Comment

by:liminal
ID: 39840926
Thanks dbaSQL, right format, but only shows me the master DB. How do I get it to show me all DBs
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 17

Accepted Solution

by:
dbaSQL earned 500 total points
ID: 39841013
oh crap.  sorry about that.  here you go:

CREATE TABLE ##RESULTS
(
    DatabaseName sysname,
    LogicalFileName sysname,
    PhysicalFileName nvarchar(500),
    FileSize decimal (18,2),
    FreeSpace decimal (18,2)
)  
EXEC sp_msforeachdb '
      Use [?];
      Insert Into ##RESULTS (DatabaseName, LogicalFileName, PhysicalFileName, FileSize, FreeSpace)
            Select DB_NAME() AS [DatabaseName], Name,  physical_name,
            Cast(Cast(Round(cast(size as decimal) * 8.0/1024.0,2) as decimal(18,2)) as nvarchar) Size,
            Cast(Cast(Round(cast(size as decimal) * 8.0/1024.0,2) as decimal(18,2)) -
                  Cast(FILEPROPERTY(name, ''SpaceUsed'') * 8.0/1024.0 as decimal(18,2)) as nvarchar) As FreeSpace
            From sys.database_files
      '
SELECT * FROM ##RESULTS

DROP TABLE ##RESULTS
0
 

Author Comment

by:liminal
ID: 39841035
HAHA, was getting a syntax error, then I realised I had copied the top line as well.

Anyway, thanks for that... Worked like a charm :)

One more question, but happy to open other question if you want the points.

We are moving to IAAS very soon, but want to know how much data actually changes in any given time period so we can work out bandwidth for replication of the databases.

Do you have a query for that? Or a recommendation of how to get that info.

Thanks again, great help :)
0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 39841067
0
 

Author Closing Comment

by:liminal
ID: 39848707
Thanks so much
0
 
LVL 17

Expert Comment

by:dbaSQL
ID: 39849065
No problem.  Hopefully I helped.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Two tables - Sum of values - What is the difference 31 54
Sql Query join multiple table and distinct records 7 31
Query 14 57
Present Absent from working date rage 11 36
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

791 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