Database size report for multiple database on multiple servers

As a DBA, you are responsible for monitoring the growth of the databases on multiple servers in different environments. There are many ways we can achieve this, my requirement was to use linked servers to get the databases size of all the linked servers and email the report to DBA group.

Here is the script i have written to get the database sizes of all the linked servers.

 

 CREATE PROCEDURE [dbo].[SumofDBsize]
AS
BEGIN
--This procedure is used to sum all the databases sizes in all the Linked servers.</pre>
IF OBJECT_ID('tempdb.dbo.#sum_volum') IS NOT NULL
BEGIN
DROP TABLE tempdb.dbo.#sum_volum;
END
ELSE
BEGIN
CREATE TABLE #sum_volum (
Server_name varchar(50),
DATABASE_NAME varchar(500),
DATABASE_SIZE bigint
)
END
DECLARE @tmp_key varchar(50)
DECLARE @db CURSOR
DECLARE @sql nvarchar(max)
SET @db = CURSOR FOR
SELECT
name 'server_name'
FROM sys.servers
ORDER BY server_id

OPEN @db
FETCH NEXT FROM @db INTO @tmp_key

WHILE (@@FETCH_STATUS = 0)
BEGIN
--select @tmp_key
SET @sql = N'insert into #sum_volum(DATABASE_NAME,DATABASE_SIZE)

select
db.name ,
DATABASE_SIZE = convert(bigint, convert(bigint, sum(s_mf.size))*8)
--REMARKS = convert(varchar(500),null)
from ' + QUOTENAME(@tmp_key) + '.master.sys.master_files s_mf right outer join ' + QUOTENAME(@tmp_key) + '.master. sys.databases db on s_mf.database_id = db.database_id
where
s_mf.state = 0
group by db.name
order by 1 ';

EXEC sp_sqlexec @sql
SELECT
@tmp_key
UPDATE #sum_volum
SET Server_name = @tmp_key
WHERE Server_name IS NULL
OR Server_name = ''
FETCH NEXT FROM @db INTO @tmp_key

END;

CLOSE @db
DEALLOCATE @db
EXEC msdb.dbo.sp_send_dbmail @profile_name = 'SUM_DB_Volumes',
@recipients = 'youremail@gmail.com',
@subject = 'SQL servers Database Volume Reports',
@body = 'Please Find the attachment for the Volume reports',
@execute_query_database = 'YourDBName',
@query = '
select server_name,DATABASE_NAME,sum(DATABASE_SIZE) ''DATABASE_SIZE''
from #sum_volum
group by server_name,DATABASE_NAME,DATABASE_SIZE
union all
select '''',''Total Size In KB'',sum(DATABASE_SIZE)
from #sum_volum
union all
select '''',''Total Size In GB'',((sum( DATABASE_SIZE )/1024)/1024)
from #sum_volum
union all
select '''',''Total Size In TB'',(((sum( DATABASE_SIZE )/1024)/1024)/1024)
from #sum_volum',

@attach_query_result_as_file = 1,
@query_result_separator = ' ',
@exclude_query_output = 1,
@query_result_no_padding = 1,
@query_result_header = 1,
@query_attachment_filename = 'SQL server volume report.csv'
END

GO 

 

Hope you enjoyed the post!

Cheers

Ramasankar Molleti

LinkedIn: LinkedIn Profile

Twitter: Twitter

Published by Ramasankar

As a Principal Cloud Architect with over 18 years of experience, I am dedicated to revolutionizing IT landscapes through cutting-edge cloud solutions. My expertise spans Cloud Architecture, Security Architecture, Solution Design, Cloud Migration, Database Transformation, Development, and Big Data Analytics.Currently, I spearhead cloud initiatives with a focus on Infrastructure, Containerization, Security, Big Data, Machine Learning, and Artificial Intelligence. I collaborate closely with development teams to architect, build, and manage robust cloud ecosystems that drive business growth and technological advancement.Core Competencies: • Cloud Platforms: AWS, Google Cloud Platform, Microsoft Azure • Technologies: Kubernetes, Serverless Computing, Microservices • Databases: MS SQL Server, PostgreSQL, Oracle, MongoDB, Amazon Redshift, DynamoDB, Aurora • Industries: Finance, Retail, Manufacturing. Throughout my career, I’ve had the privilege of working with industry leaders such as OCC, Gate Gourmet, Walgreens, and Johnson Controls, gaining invaluable insights across diverse sectors.As a lifelong learner and knowledge sharer, I take pride in being the first in my organization to complete all major AWS certifications. I am passionate about mentoring and guiding fellow professionals in their cloud journey, fostering a culture of continuous learning and innovation.Let’s connect and explore how we can leverage cloud technologies to transform your business: • LinkedIn: https://www.linkedin.com/in/ramasankar-molleti-23b13218/ • Book a mentorship session: [1:1] Together, let’s architect the future of cloud computing and drive technological excellence. Disclaimer The views expressed on this website/blog are mine alone and do not reflect the views of my company. All postings on this blog are provided “AS IS” with no warranties, and confers no rights. The owner of https://ramasankarmolleti.com will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.

Leave a comment