The fastest way to get log space information for all the databases under a SQL Server instance is to use the following T-SQL command:
DBCC SQLPERF(LOGSPACE)
The above command returns a record for each database under the current SQL Server instance which contains the following information:
– Database Name
– Log Size (MB)
– log Space Used (%)
– Status
Additionally, if you like to process this information, you can do so by first storing it into a temporary table.
You can do this as follows:
–Step 1: If temporary table exists, then drop it
IF Object_id(N’tempdb..#tempTbl’) IS NOT NULL
DROP TABLE #temptbl
–Step 2: Create temporary table
CREATE TABLE #temptbl
( dbname VARCHAR(250),
logsize FLOAT,
logspaceused FLOAT,
status INT
)
–Step 3: Populate temporary table with log size information
INSERT INTO #tempTbl
EXEC(‘DBCC SQLPERF(LOGSPACE)’)
–Step 4: Process the temporary table
–Examples:
SELECT *
FROM #tempTbl
ORDER BY logsize DESC
SELECT *
FROM #tempTbl
ORDER BY logspaceused ASC
I hope you enjoyed the post as much as I did writing it! 🙂
Artemakis Artemiou is a Senior SQL Server Architect, Author, a 9 Times Microsoft Data Platform MVP (2009-2018). He has over 20 years of experience in the IT industry in various roles. Artemakis is the founder of SQLNetHub and {essentialDevTips.com}. Artemakis is the creator of the well-known software tools Snippets Generator and DBA Security Advisor. Also, he is the author of many eBooks on SQL Server. Artemakis currently serves as the President of the Cyprus .NET User Group (CDNUG) and the International .NET Association Country Leader for Cyprus (INETA). Moreover, Artemakis teaches on Udemy, you can check his courses here.
I prefer to use sys.master_files and FILEPROPERTY([file_name], 'spaceused') to get the space used. Yes, you need to convert from pages to MB, but that's an easy conversion.
Just goes to show that there is always more than 1 way to do things in SQL Server
Hi Jack,
Thank you for your comment.
Yes, of course! That's one of the things I really like in SQL Server; it allows you to do the same thing using different ways! This is what I call flexibility!
Cheers