SQL Server DB Owner Alert:How to Automatically Notify If Databases Are Not Owned by SA

preview_player
Показать описание
Join this channel to get access to perks:

Join the Telegram Group for the scripts and assistance:

You Can Also Download the scripts from below folder
We strongly believe there is always chance of betterment, so suggestions are most welcome.

Happy learning, and All the Best in your professional journey!

The journey of improvement is ongoing and never be an end.

Connect With me,

#azuresql #azure #sqldba #sqlserverdba #sql #sqlserver #sqlserverdeveloper #performance #performancetuning #performanceoptimization #mssql #mssqlserver #mssqlserverdba

Thank you!
MS SQL DBA Tech Support
Рекомендации по теме
Комментарии
Автор

DECLARE @NonSaDBs TABLE (
DBName NVARCHAR(128),
DBOwner NVARCHAR(128)
);

INSERT INTO @NonSaDBs (DBName, DBOwner)
select Name, suser_sname(owner_sid) AS DbOwner from sys.databases
WHERE suser_sname(owner_sid) <> 'sa'

Declare @subject Varchar(200)

IF EXISTS (SELECT 1 FROM @NonSaDBs)
BEGIN
DECLARE @DBList NVARCHAR(MAX) = '';

SELECT @DBList = @DBList +
'<tr><td>' + DBName + '</td><td>' + DBOwner + '</td></tr>'
FROM @NonSaDBs;

DECLARE @body NVARCHAR(MAX);
SET @body = '<html>' +
'<head>' +
'<style>' +
'table {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse; font-size: 12px; width: auto;}' +
'th {border-width: 1px; padding: 2px; border-style: solid; border-color: black; background-color: Pink; font-size: 12px; white-space: nowrap;}' +
'td {border-width: 1px; padding: 2px; border-style: solid; border-color: black; font-size: 12px; white-space: nowrap;}' +
'h1 {font-size: 16px;}' +
'</style>' +
'</head>' +
'<body>' +
'<p>The following Databases are Not Owned by sa:</p>' +
'<table>' +
'<tr><th>DB Name</th><th>Job Owner</th></tr>' +
@DBList +
'</table>' +
'</body>' +
'</html>';

-- Send alert email
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DBATEAM', -- Change to your Database Mail profile name
@subject = @subject,
@body = @body,
@body_format = 'HTML'; -- Specify that the body is HTML
END;

MSSQLDBATechSupport