Welcome to SQLDBANow.com! This blog, created by Bandaru Ajeyudu, is dedicated to learning and sharing knowledge about SQL DBA and Azure SQL. Join us as we explore insights, tips, and best practices in the world of SQL Database Administration and Azure SQL.
Interview Questions on AOAG in SQL server
Send Email for Service Restarts in SQL server
--Send Email for Service Restarts in SQL server
USE msdb
GO
-- Declare variables for necessary email content
DECLARE @ServerName VARCHAR(128),
@ComputerNamePhysicalNetBIOS VARCHAR(128),
@Datetime DATETIME,
@EmailRecipients VARCHAR(512),
@EmailSubject VARCHAR(128),
@MessageBody VARCHAR(512)
-- Set variables to proper values
SELECT @ComputerNamePhysicalNetBIOS = CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS VARCHAR(128)),
@ServerName = CAST(SERVERPROPERTY('ServerName') AS VARCHAR(128)),
@Datetime = GETDATE(),
@EmailRecipients = 'bajeyudu@sqldbanow.com', -- if more than one email address use ; between email addresses
@EmailSubject = 'SQL Server Services Have Been Started!!!'
SELECT @MessageBody = 'SQL Server services have been started on a SQL Server Instance named ' + @ServerName + CHAR(13) +
'running on windows server ' + @ComputerNamePhysicalNetBIOS + '.' + CHAR(13) + CHAR(13) +
'Investigate the service restart if it has not been communicated.'
EXEC sp_send_dbmail
@recipients = @EmailRecipients,
@subject = @EmailSubject,
@body = @MessageBody,
@body_format = 'TEXT'
Suspect Pages Alert in SQL Server
--By using below mentioned script will get suspect pages report via email
declare @count integer
declare @tableHTML nvarchar(MAX);
declare @subj nvarchar(100)
select @count=count(1)
from msdb.dbo.suspect_pages;
set @subj = 'Suspect Pages Found in ' + @@SERVERNAME;
set @tableHTML =
N'<H1>Suspect Pages Found in ' + @@SERVERNAME + ', details are below.</H1>' +
N'<table border="1">' +
N'<tr><th>Database ID</th><th>Database</th>' +
N'<th>File ID</th><th>File</th><th>Page ID</th>' +
N'<th>Event Desc</th><th>Error Count</th><th>Last Updated</th></tr>' +
cast ( ( select td = sp.database_id, '',
td = d.name, '',
td = sp.file_id, '',
td = mf.physical_name, '',
td = sp.page_id, '',
td = case when sp.event_type = 1 then '823 or 824 error other than a bad checksum or a torn page'
when sp.event_type = 2 then 'Bad checksum'
when sp.event_type = 3 then 'Torn Page'
when sp.event_type = 4 then 'Restored (The page was restored after it was marked bad)'
when sp.event_type = 5 then 'Repaired (DBCC repaired the page)'
when sp.event_type = 7 then 'Deallocated by DBCC'
end, '',
td = sp.error_count, '',
td = sp.last_update_date
from msdb.dbo.suspect_pages sp
inner join sys.databases d on d.database_id=sp.database_id
inner join sys.master_files mf on mf.database_id=sp.database_id and mf.file_id=sp.file_id
for xml path('tr'), TYPE
) as nvarchar(max) ) +
N'</table>' ;
IF @count > 0
exec msdb.dbo.sp_send_dbmail
@recipients=N'bandaruajeyudu@outlook.com',
@body= @tableHTML,
@subject = @subj,
@body_format = 'HTML',
@profile_name ='SQLMail'
GO
delete from msdb.dbo.suspect_pages
where last_update_date < getdate()-90;
Monitoring SQL Server Jobs: Script to Report Failed Jobs
Managing and monitoring SQL Server jobs is crucial for maintaining the health and performance of your SQL Server environment. One of the key aspects is to ensure that failed jobs are promptly reported and addressed. Below is a script that creates a report of failed SQL Server jobs and sends it via email. This script can be particularly useful for DBAs to automate the monitoring process.
Script Overview
The script performs the following tasks:
- Create a Table: A temporary table
Failed_Jobs
is created to store details of the failed jobs. - Insert Data: Job failure details are inserted into the
Failed_Jobs
table. This includes jobs that failed in the last 24 hours. - Check for Failures: The script checks if there are any failed jobs.
- Send Email: If there are any failures, an HTML-formatted email is sent, listing the details of the failed jobs.
- Drop Table: The temporary table is dropped at the end of the script.
Here’s the complete script:
Automatic alert when cluster fail over happened
When fail over happen, both SQL server and Agent service will get restarted, Then we will get Notification Alert When Fail Over Happened
DECLARE
@importance AS VARCHAR(6)
DECLARE
@body AS NVARCHAR(1000)
DECLARE
@Subject AS NVARCHAR(1000)
DECLARE
@InstanceName AS VARCHAR(100)DECLARE @NodeName AS NVARCHAR(100)
DECLARE
@recipients VARCHAR(100)
SELECT
@InstanceName =@@SERVERNAME
SELECT
@NodeName = CAST(SERVERPROPERTY('ComputerNamePhysicalNetBIOS') AS NVARCHAR(200))
SELECT
@Subject = 'Fail
over Happened for Instance '+@InstanceName
SELECT
@body = 'Fail over
Happened for Instance '+@InstanceName + '. This instance is
currently running on the node '+@NodeName
SELECT
@importance ='High'
EXEC
msdb.dbo.sp_send_dbmail
@profile_name ='sqlmail',
@recipients ='bajeyudu@SQLDBANOW.com;',
--@recipients=@recipientsList,
@subject = @subject ,
@body = @body,
@body_format = 'HTML'
,
@importance=@importance