locked
Alert mail from SQL server RRS feed

  • Question

  • User-809753952 posted

    I want to send an alert mail from SQL Server, when the number of records in a table reaches a specific number.

    For example the table has one million records in the start.  Some records are deleted every day. When the number of records in the table reaches 20000, an alert mail should be sent.

    Tuesday, December 8, 2015 12:28 PM

Answers

  • User1633621018 posted

    Hi,

    Use below example

    IF (SELECT COUNT(*) FROM whatever) > 2000
     BEGIN
     EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'Adventure Works Administrator',
        @recipients = 'danw@Adventure-Works.com',    
        @subject = 'Work Order Count',
        @attach_query_result_as_file = 1 ;
    
     END

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2015 12:34 PM
  • User753101303 posted

    Hi,

    You could schedule a job to do various checks. See https://msdn.microsoft.com/en-us/library/ms189635.aspx to configure and use datatabase mail and https://technet.microsoft.com/en-us/library/ms135739(v=sql.105).aspx to create a job (in short create a procedure doing that and then you'll just create job calling it on a regular basis).

    Try to be more specific if you have a particular problem when trying to apply that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2015 12:37 PM

All replies

  • User1633621018 posted

    Hi,

    Use below example

    IF (SELECT COUNT(*) FROM whatever) > 2000
     BEGIN
     EXEC msdb.dbo.sp_send_dbmail
        @profile_name = 'Adventure Works Administrator',
        @recipients = 'danw@Adventure-Works.com',    
        @subject = 'Work Order Count',
        @attach_query_result_as_file = 1 ;
    
     END

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2015 12:34 PM
  • User753101303 posted

    Hi,

    You could schedule a job to do various checks. See https://msdn.microsoft.com/en-us/library/ms189635.aspx to configure and use datatabase mail and https://technet.microsoft.com/en-us/library/ms135739(v=sql.105).aspx to create a job (in short create a procedure doing that and then you'll just create job calling it on a regular basis).

    Try to be more specific if you have a particular problem when trying to apply that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 8, 2015 12:37 PM