Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-17
3
Medium Priority
?
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

670 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