Alert mail sps when table got inserted in sql server

To send an alert to a specific email address (e.g. "[email protected]") whenever a table is inserted in a SQL Server database, you can use a combination of SQL Server features:

  1. SQL Server Agent: This is a built-in feature in SQL Server that allows you to schedule jobs, including sending emails.
  2. SQL Server Triggers: These are database objects that automatically execute a set of statements when a specific event occurs, such as an insert operation.
  3. SMTP: This is a protocol for sending emails.

Here's a step-by-step guide to achieve this:

Step 1: Create a SQL Server Agent Job

  1. Open SQL Server Management Studio (SSMS) and connect to your SQL Server instance.
  2. In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server Agent" and select "New Job".
  3. In the "New Job" dialog box, enter a job name (e.g. "Insert Alert Job") and a description.
  4. Click "OK" to create the job.

Step 2: Create a SQL Server Trigger

  1. In the Object Explorer, expand the "Databases" node, then right-click on the database that contains the table you want to monitor (e.g. "MyDatabase").

  2. Select "New" > "Trigger" > "Table Trigger".

  3. In the "New Trigger" dialog box, enter a trigger name (e.g. "InsertTrigger") and a description.

  4. Select the table you want to monitor and the type of trigger you want to create (e.g. "Insert").

  5. In the "Trigger" tab, enter the following code:

    CREATE TRIGGER InsertTrigger
    ON MyTable
    AFTER INSERT
    AS
    BEGIN
     DECLARE @email nvarchar(255) = '[email protected]';
     DECLARE @subject nvarchar(255) = 'Insert Alert';
     DECLARE @body nvarchar(max) = 'A new record has been inserted into MyTable.';
    
     EXEC msdb.dbo.sp_send_dbmail
         @profile_name = 'Your Email Profile',
         @recipients = @email,
         @subject = @subject,
         @body = @body;
    END;

    This trigger will send an email to the specified email address whenever a new record is inserted into the "MyTable" table.

Step 3: Configure the SQL Server Agent Job

  1. In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server Agent" and select "Properties".
  2. In the "SQL Server Agent" dialog box, click on the "Jobs" tab.
  3. Select the job you created earlier (e.g. "Insert Alert Job") and click "Edit".
  4. In the "Job Properties" dialog box, click on the "Steps" tab.
  5. Click "New" to create a new step.
  6. In the "New Job Step" dialog box, enter a step name (e.g. "Send Email") and a description.
  7. Select "T-SQL" as the step type and enter the following code:
    EXEC msdb.dbo.sp_send_dbmail
     @profile_name = 'Your Email Profile',
     @recipients = '[email protected]',
     @subject = 'Insert Alert',
     @body = 'A new record has been inserted into MyTable.';

    This step will execute the trigger code and send the email.

Step 4: Configure the Email Profile

  1. In the Object Explorer, expand the "SQL Server" node, then right-click on "SQL Server Agent" and select "Properties".
  2. In the "SQL Server Agent" dialog box, click on the "Database Mail" tab.
  3. Click "New" to create a new email profile.
  4. In the "New Database Mail Profile" dialog box, enter a profile name (e.g. "Your Email Profile") and a description.
  5. Enter the email server settings (e.g. SMTP server, username, password).

Step 5: Save and Schedule the Job

  1. Click "OK" to save the job and step.
  2. In the "SQL Server Agent" dialog box, click on the "Schedules" tab.
  3. Click "New" to create a new schedule.
  4. In the "New Job Schedule" dialog box, enter a schedule name (e.g. "Insert Alert Schedule") and a description.
  5. Select the job you created earlier (e.g. "Insert Alert Job") and the frequency you want the job to run (e.g. "Once").
  6. Click "OK" to save the schedule.

Now, whenever a new record is inserted into the "MyTable" table, the trigger will execute and send an email to the specified email address using the SQL Server Agent job.