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 8334403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:20:22+00:00 2026-06-09T03:20:22+00:00

I have 2 tables like so: JOBS table Jobcode UserId Status 101 130 R

  • 0

I have 2 tables like so:

JOBS table

Jobcode    UserId    Status
101        130       R
102        139       D

USERS table

UserId    Email
130       test@example.com

I want to create a trigger on insert and update that sends an email to my stored procedure:

EXEC dbo.SendMyEmail @email, @jobcode;

when the jobcode is inserted as ‘D’ or updated to ‘D’.

  • 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-09T03:20:24+00:00Added an answer on June 9, 2026 at 3:20 am

    I’m not sure about data types etc but this should at least put you on the right track.
    Hope it helps…

    CREATE TRIGGER SendEmailOnStatusD
    
    ON JOBS
    
    -- trigger is fired when an update is made for the table
    FOR UPDATE --You can add the same for INSERT
    
    AS
    
        -- holds the UserID so we know which Customer was updated
        DECLARE @UserID int
        DECLARE @JobCode int
    
        SELECT @UserID = UserId, @JobCode = JobCode 
        FROM INSERTED WHERE [Status] = 'D' --If you want the old value before the update, use 'deleted' table instead of 'inserted' table
    
        IF (@UserID IS NOT NULL)
        BEGIN
    
            -- holds the email
            DECLARE @email varchar(250)
    
            SELECT @email = Email FROM USERS WHERE UserId = @UserID
    
            EXEC SendMyEmail (@email, @jobcode);
    
        END
    
    GO
    

    EDIT:

    Above code does not handle multiple updates, so for better practice see below option

    CREATE TRIGGER SendEmailOnStatusD ON JOBS
    
    -- trigger is fired when an update is made for the table
    FOR UPDATE --You can add the same for INSERT
    
    AS
    
        DECLARE @Updates table(UserID int, JobCode int, Email varchar(250))
    
        INSERT INTO @Updates (UserID, JobCode, Email)
        SELECT i.UserID, i.JobCode, u.Email
        FROM INSERTED i
            JOIN USERS u ON i.UserID = u.UserID
        WHERE [Status] = 'D'
    
        DECLARE @UserID int
        DECLARE @JobCode int
        DECLARE @Email varchar(250)
    
        WHILE EXISTS(SELECT * FROM @Updates)
        BEGIN
    
            SELECT TOP 1
                @UserID = UserID,
                @Email = Email, 
                @JobCode = JobCode
            FROM @Updates WHERE UserID = @UserID
    
            EXEC SendMyEmail (@email, @jobcode);
    
            DELETE FROM @Updates
            WHERE UserID = @UserID
    
        END
    
    GO
    

    Additionally, as discussed in the comments, sending emails from a trigger is also not the best, but as this is what the question asks for it has been included. I would recommend alternative options for sending emails such as a queue which has been mentioned in other answers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have two tables like, CREATE TABLE [dbo].[entry] ( [id] [int] NULL, [service] [int]
I have 2 tables like this: Stock Table product_id bigint(20) qty float Sales Table
I have three tables like this: Person table: person_id | name | dob --------------------------------
i have four tables like below in sql server 2008 : TABLE 1 ->
i have a table with jobs like this in a MS SQL Server 2008
We have a DB like this: CREATE TABLE `jobs` ( `id` int NOT NULL
I have 2 tables: posts tags Tags table is structured like this: post_id tag
I have defined my table like this: <div id=a> <table id=main_table border=1> <tr> <th>Jobs</th>

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.