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

x
?
Solved

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

Posted on 2010-11-17
3
Medium Priority
?
474 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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

971 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