Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Query database location and size

Posted on 2014-02-06
8
Medium Priority
?
321 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 23

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
LVL 17

Accepted Solution

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

926 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