Solved

MS SQL 2008 DB Size Report for SpaceUsed in a DB file

Posted on 2010-11-17
3
460 Views
Last Modified: 2012-05-10
Here is what I have so far that works.
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Administrator',
@recipients = 'administrator@somebody.com,
@query = 'USE TopsData
SELECT DB_NAME() AS DbName,
name AS FileName,
size/128.0 AS CurrentSizeMB,  
size/128.0 - CAST(FILEPROPERTY(name, ''SpaceUsed'') AS INT)/128.0 AS FreeSpaceMB
FROM sys.database_files;',
@subject = "Error Log",
@attach_query_result_as_file = 1

I want a 3rd result that tells me the SpaceUsed in the DB mdf file (CurrentSizeMB - FreeSpaceMB = UsedSpaceMB) without the transaction log data.
Soo...it will look something like below:
CurrentSizeMB    FreeSpaceMB   UsedSpaceMB
TIA
0
Comment
Question by:NetManaged
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 50 total points
ID: 34156722
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Administrator',
@recipients = 'administrator@somebody.com,
@query = 'USE TopsData
SELECT DB_NAME() AS DbName, 
name AS FileName, 
size/128.0 AS CurrentSizeMB,  
size/128.0 - CAST(FILEPROPERTY(name, ''SpaceUsed'') AS INT)/128.0 AS FreeSpaceMB,
CAST(FILEPROPERTY(name, ''SpaceUsed'') AS INT)/128.0 AS SpaceUsed
FROM sys.database_files;',
@subject = "Error Log",
@attach_query_result_as_file = 1

Open in new window


If you want to exclude the log then.

EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Administrator',
@recipients = 'administrator@somebody.com,
@query = 'USE TopsData
SELECT DB_NAME() AS DbName, 
name AS FileName, 
size/128.0 AS CurrentSizeMB,  
size/128.0 - CAST(FILEPROPERTY(name, ''SpaceUsed'') AS INT)/128.0 AS FreeSpaceMB,
CAST(FILEPROPERTY(name, ''SpaceUsed'') AS INT)/128.0 AS SpaceUsed
FROM sys.database_files
WHERE NOT type_desc='LOG';',
@subject = "Error Log",
@attach_query_result_as_file = 1

Open in new window

0
 

Author Comment

by:NetManaged
ID: 34175788
EvilPostit,
     Using your first example (only modifed the e-mail address before the .com), I recieved the below result in the SSMS 2008 Messages pane:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'SpaceUsed'.
Msg 105, Level 15, State 1, Line 10
Unclosed quotation mark after the character string ',
@subject = "Error Log",
@attach_query_result_as_file = 1

Editing your first example to the below using only single quotes for SpaceUSed (ie. 'SpaceUsed'. The query editor was claiming incorrect syntax with two single quotes [''SpaceUSed''] and using double-quotes resulted in invalid column name),  Looks like this:
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Administrator',
@recipients = 'administrator@somebody.com,
@query = 'USE TopsData
SELECT DB_NAME() AS DbName,
name AS FileName,
size/128.0 AS CurrentSizeMB,  
size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS FreeSpaceMB,
CAST(FILEPROPERTY(name, 'SpaceUsed') AS INT)/128.0 AS SpaceUsed
FROM sys.database_files;',
@subject = "Error Log",
@attach_query_result_as_file = 1
This resulted in the following when running the query:
Msg 105, Level 15, State 1, Line 10
Unclosed quotation mark after the character string ',
@subject = "Error Log",
@attach_query_result_as_file = 1

Suggestions?
0
 

Author Comment

by:NetManaged
ID: 34176203
EvilPostit,
                 NM, put it down to user error during copy/paste process.
Works great!!!

Thanks!

Greg
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now