Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7980815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:11:08+00:00 2026-06-04T10:11:08+00:00

I am trying to develop a mail trigger. Could someone assist on how this

  • 0

I am trying to develop a mail trigger. Could someone assist on how this could be achieved so that when a use inserts a record it check the “speed” field such that when the inserted value exceeds 100, a mail is send to the specified address.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:11:11+00:00Added an answer on June 4, 2026 at 10:11 am

    First you need to set up database mail – if you haven’t done so, this question might help:

    • Scripting setup of database mail

    Then you need a trigger:

    CREATE TRIGGER dbo.whatever
    ON dbo.wherever
    FOR INSERT
    AS
    BEGIN
        SET NOCOUNT ON;
    
        IF EXISTS (SELECT 1 FROM inserted WHERE speed > 100)
        BEGIN
            EXEC msdb.dbo.sp_send_dbmail
              @recipients = 'whoever@yourcompany.com', 
              @profile_name = 'default',
              @subject = 'Someone was speeding', 
              @body = 'Yep, they sure were.';
        END
    END
    GO
    

    Now, you’re probably going to say you want data from the insert to be actually be included in the e-mail. And your first inclination is going to be to declare some local variables and assign them from inserted – this doesn’t work because your trigger could be responding to a multi-row insert. So the right way to do this is:

    CREATE TRIGGER dbo.whatever
    ON dbo.wherever
    FOR INSERT
    AS
    BEGIN
        SET NOCOUNT ON;
    
        DECLARE @body NVARCHAR(MAX) = N'';
    
        SELECT @body += CHAR(13) + CHAR(10) + RTRIM(some_col) FROM inserted;
    
        IF EXISTS (SELECT 1 FROM inserted WHERE speed > 100)
        BEGIN
            EXEC msdb.dbo.sp_send_dbmail
              @recipients = 'whoever@yourcompany.com', 
              @profile_name = 'default',
              @subject = 'At least one person was speeding', 
              @body = @body;
        END
    END
    GO
    

    That all said, I am not a big fan of sending e-mail from a trigger. Even though database mail uses service broker and so is asynchronous, I would be much more inclined to populate a queue table, and have a background thread that comes around and sends all of the appropriate e-mails. The twothree nice things about this are:

    1. you minimize the potential delays in committing the outer transaction that fired the trigger – the more complicated your logic in the trigger, the slower you make that process.
    2. since it is probably not essential that the e-mail is sent the microsecond the row is inserted, you can easily fluctuate the timing of the background process – this avoids having to check the table very minute, all day, when very few times it will ever have to actually do anything.
    3. As @goodeye pointed out, keeping this process separate can prevent errors in the e-mail part of the process from interfering with the original DML (in their case, an invalid parameter to sp_send_dbmail – which I inadvertently suggested – prevented the insert).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a class that supports an asynchronus method invocation. This is
Trying to develop using MVVM: I have this Csla.PropertyStatus control that is created in
I am trying to develop an Android application and I heard that for someone
im trying to develop an app for a win CE mobile device that downloads
I am trying to develop a BlackBerry application that will show data from an
I'm trying to develop a JS function that creates a new row each time
I have a server (Java) that is running on UNIX. I'm trying to develop
I'd been trying to develop one with this for a long time. I'd try
I am trying to develop a mobile app, that registered with a Java program
I am trying to develop application to send mail using configured SMTP client rather

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.