sqlschool.gr logo

articles

Articles of SQLschool.gr Team

Common Alerts that each SQL Server instance must have

Antonios Chatzipavlis
Monday 28 June 2021

Overview

Η καθημερινότητα κρύβει εκπλήξεις που θα εμφανιστούν αργότερα και τότε ίσως τα πράγματα να είναι χειρότερα από την στιγμή που η έκπληξη εμφανίστηκε για πρώτη φορά. Για αυτό κάθε DBA που μισεί τις δυσάρεστες εκπλήξεις και ιδιαίτερα αυτές που έρχονται από το παρελθόν πρέπει να κάνει με κάποιο τρόπο τον SQL Server να του μιλάει.

Σύμμαχος του σε αυτό είναι το Database Mail, ο SQL Server Agent και τα Alerts.

Για τα πρώτα δύο αρκεί να τα έχουμε ενεργοποιημένα (υπάρχουν σχετικά άρθρα για αυτά στο SQLschool.gr).

Για τα alerts όμως πάντα τίθεται το ερώτημα ποια πρέπει να συμπεριληφθούν.

Όλοι όσοι τόσα χρόνια ασχολούμαστε με τον SQL Server έχουμε καταλήξει στο ποια πρέπει να είναι αυτά και πάντα τα ενεργοποιούμε στο instance ώστε να έρχονται άμεσα σε εμάς τα σχετικά email ώστε να είμαστε σε θέση άμεσα να αντιμετωπίσουμε το πρόβλημα.

Script

Επειδή κανείς δεν θέλει με χειροκίνητο τρόπο να δημιουργεί αυτά, το παρακάτω script κάνει την δουλειά αυτή άμεσα, όποτε πλέον δεν υπάρχει δικαιολογία για να μην υπάρχουν σε κάθε SQL Server instances.

Το μόνο που χρειάζεται είναι να έχετε ενεργοποιημένα το Database Mail, το SQL Agent service και στο script να ορίσεται τις τιμές στα variables που ζητάνε αυτό και απλά να το εκτελέσετε.

Πλέον θα είστε να θέση να μην έχετε άγχος για το τι θα ξημερώσει. Αν είναι να έρθει θα έρθει άμεσα αλλιώς όλα είναι καλά.

SQL Script

USE [msdb];


declare @operator_name nvarchar(20) = N'**<set operator name>**';        
declare @operator_email_address nvarchar(100) = N'**<set operator email>**';
declare @notmsg nvarchar(200) = N'Server: ' + @@Servername ;

-- create operator

EXEC msdb.dbo.sp_add_operator @name=@operator_name, 
        @enabled=1, 
        @weekday_pager_start_time=90000, 
        @weekday_pager_end_time=180000, 
        @saturday_pager_start_time=90000, 
        @saturday_pager_end_time=180000, 
        @sunday_pager_start_time=90000, 
        @sunday_pager_end_time=180000, 
        @pager_days=0, 
        @email_address=@operator_email_address,
        @category_name=N'[Uncategorized]'

-- create severity alerts

declare @sf int = 19, @st int = 25;
declare @alrt_name nvarchar(20) = '';

WHILE ( @sf<=@st )
BEGIN

    SET @alrt_name = N'Severity_'+cast(@sf as nvarchar(2));
    
    EXEC msdb.dbo.sp_add_alert @name=@alrt_name, 
            @message_id=0, 
            @severity=@sf, 
            @enabled=1, 
            @delay_between_responses=0, 
            @include_event_description_in=1, 
            @notification_message=@notmsg,
            @job_id=N'00000000-0000-0000-0000-000000000000';
    
    EXEC msdb.dbo.sp_add_notification @alert_name=@alrt_name, @operator_name=@operator_name, @notification_method = 1;

    SET @sf+=1;
    
END

-- create specific errors alerts
IF OBJECT_ID('tempdb..#ae') is not null
BEGIN
    DROP TABLE #ae;
END;

-- errors 
CREATE TABLE #ae ( err_number int );
INSERT INTO #ae 
VALUES    (823),(824),(825),(802),(845),(1101),(1105)
         ,(1121),(1214),(9002),(2508),(2511),(3271)
         ,(5228),(5229),(5242),(5243),(5250),(5901)
         ,(17130),(17300) ;


DECLARE c CURSOR READ_ONLY
FOR SELECT err_number FROM #ae ORDER BY err_number;

DECLARE @err int, @ea_name nvarchar(20);

OPEN c;

FETCH NEXT FROM c INTO @err
WHILE (@@fetch_status <> -1)
BEGIN
    IF (@@fetch_status <> -2)
    BEGIN

        SET @ea_name = N'Error_'+RIGHT('_____'+CAST(@err AS nvarchar(5)),5);

        EXEC msdb.dbo.sp_add_alert @name=@ea_name, 
                @message_id=@err, 
                @severity=0, 
                @enabled=1, 
                @delay_between_responses=0, 
                @include_event_description_in=1, 
                @notification_message=@notmsg, 
                @category_name=N'[Uncategorized]', 
                @job_id=N'00000000-0000-0000-0000-000000000000'

        EXEC msdb.dbo.sp_add_notification @alert_name=@ea_name, @operator_name=@operator_name, @notification_method = 1;

    END
    FETCH NEXT FROM c INTO @err
END

CLOSE c
DEALLOCATE c
GO

//Antonios Chatzipavlis


Antonios Chatzipavlis

Antonios Chatzipavlis

Antonios Chatzipavlis is a highly experienced Data Solutions Consultant and Trainer. He has been working in the IT industry since 1988, holding various roles such as senior developer, IT Manager, Data & AI Solutions Architect and Consultant.

Since 1995, Antonios has focused on modern technologies and software development tools, primarily by Microsoft. He has specialized in Data & AI since 2000, with expertise in Microsoft Data Platform (SQL Server, Azure SQL Databases, Azure Synapse Analytics, Microsoft Fabric, Power BI, AI) and Databricks.

Antonios is also a Microsoft Certified Trainer (MCT) for over 25 years, has been recognized as a Microsoft Most Valuable Professional (MVP) in Data Platform since 2010 and he is in the Data Expert 40 Powerlist 2024 by Boussias. He is the co-founder and visionary behind XLYTiCA, a company dedicated to Data & AI solutions.

Episode

Task Flows in Microsoft Fabric

image

More Episodes...

Tip

Get Certified: Become a Fabric Data Engineer

More Tips...

Become a member

If you want to receive updates from us become a member to our community.

Connect

Explore

Learn

sqlschool.gr © 2010-2025 All rights reserved

This site uses cookies for operational and analytics purposes only. By continuing to browse this site, you agree to their use.