Thursday, April 6, 2017

T-SQL to get Get Database Files Usage information

Here is an handy T-SQL script to get Database files usage Information
DECLARE @FreeSpaceThreshold int
SET @FreeSpaceThreshold = 20 -- Change this Threshold Value. Any number between 0 to 100

CREATE TABLE #GetDetails
( 
 ServerName nvarchar(100),
 DatabaseName nvarchar(100),
 LogicalFileName nvarchar(100),
 FileType nvarchar(10),
 FileLocation nvarchar(max),
 [FileSize(MB)] decimal (15,2),
 [UsedSpace(MB)] decimal (15,2),
 [FreeSpace(MB)] decimal (15,2),
 [UsedSpace(%)] decimal (15,2),
 [FreeSpace(%)] decimal (15,2)
 )

EXEC sp_MSforeachdb 'Use [?];
INSERT INTO #GetDetails
SELECT @@SERVERNAME AS ServerName,
    DB_NAME() AS DatabaseName,
    name AS LogicalFileName, 
    type_desc AS FileType, 
    physical_name AS FileLocation,
    CAST(size/128.0 AS decimal(15,2)) AS [FileSize(MB)],
    CAST(FILEPROPERTY(name, ''SpaceUsed'')/128.0 AS decimal(15,2)) AS [UsedSpace(MB)],
    CAST(size/128.0 AS decimal(15,2)) - CAST(FILEPROPERTY(name, ''SpaceUsed'')/128.0 AS decimal(15,2)) AS [FreeSpace(MB)],
    CAST(((FILEPROPERTY(name, ''SpaceUsed'')/128.0 ) /  (size/128.0))*100 AS decimal(15,2)) AS [UsedSpace(%)],
    CAST((((size/128.0) - FILEPROPERTY(name, ''SpaceUsed'')/128.0 ) /  (size/128.0))*100 AS decimal(15,2)) AS [FreeSpace(%)]
FROM sys.database_files'

SELECT * FROM #GetDetails Where [FreeSpace(%)] < @FreeSpaceThreshold
DROP TABLE #GetDetails

Download this script from https://gallery.technet.microsoft.com/T-SQL-to-Get-Database-f16f60c7?redir=0

15 comments:

  1. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts, have a nice weekend!

    Data Science Training in Bangalore

    ReplyDelete
  2. Despite working with a complex workflow top product design firms, they were able to improve and understand the process very quickly.

    ReplyDelete
  3. They also branded the website, which was required to have a seamless UX/UI while maintaining sophisticated functionalities.
    brand building firm

    ReplyDelete
  4. Organized and communicative mobile design agencies, they are efficient time managers who spare no effort to deliver practical solutions

    ReplyDelete
  5. Communication was handled through appropriate channels. They were very reliable, open, and proactive.
    branding design agency

    ReplyDelete
  6. Each time I used to always check blog posts within the first hours in the break of day, because I like to get information increasingly more.
    UI UX design agency

    ReplyDelete
  7. Cool blog site friend I'm about to suggest this to all my listing contacts.
    companies that create logos

    ReplyDelete
  8. This is one of the most important blogs that I have seen, keep it up!
    web agency

    ReplyDelete
  9. Wonderful, just what a blog it is! This blog has provided the helpful data to us continue the good work.
    UI UX company

    ReplyDelete
  10. You fully match our expectation and the selection of our data.
    UI UX design agency

    ReplyDelete
  11. Osm blog site dear I'm about to suggest this to all my listing contacts.
    Digital Marketing company in noida


    ReplyDelete

  12. Nice blog site dear I'm about to suggest this to all my listing contacts.
    Bigpond webmail support

    ReplyDelete
  13. Thank you, for providing these tips. Personally,
    I believe the best strategy is to repurpose the old content into a new useful one.

    ReplyDelete