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

  • Home
  • SEARCH
  • 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 7763799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:44:59+00:00 2026-06-01T14:44:59+00:00

I have this trigger which works functionally (as far as I can tell by

  • 0

I have this trigger which works functionally (as far as I can tell by my tests) but I strongly suspect I can simplify the code by using IF UPDATE… I just don’t fully understand how it works. I seem to do best undestanding triggers in terms of the inserted and deleted tables.

Can this trigger be simplified in the way I just mentioned (or any other way that improves the readability)?

CREATE TRIGGER [dbo].[tr_Affiliate_IU] 
   ON  [dbo].[Affiliate] 
   AFTER INSERT, UPDATE
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Get the current date.
    DECLARE @getDate DATETIME = GETDATE()

    -- Set the initial values of date_created and date_modified.
    UPDATE
        dbo.Affiliate
    SET 
         date_created = @getDate
    FROM
        dbo.Affiliate A 
        INNER JOIN INSERTED I ON A.id = I.id
        LEFT OUTER JOIN DELETED D ON I.id = D.id
    WHERE
        D.id IS NULL

    -- Ensure the value of date_created does never changes.
    -- Update the value of date_modified to the current date.
    UPDATE
        dbo.Affiliate
    SET
         date_created = D.date_created
        ,date_modified = @getDate
    FROM 
        dbo.Affiliate A 
        INNER JOIN INSERTED I ON A.id = I.id
        INNER JOIN DELETED D ON I.id = D.id 
END
  • 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-01T14:45:01+00:00Added an answer on June 1, 2026 at 2:45 pm

    If updated(column_name) cannot help as it merely states that column participated in a query that raised the trigger. The update part of the trigger can be shortened by use of case statement to help decide which date date_created is going to hold.

    CREATE TRIGGER [dbo].[tr_Affiliate_IU] 
       ON  [dbo].[Affiliate] 
       AFTER INSERT, UPDATE
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
    
        -- Get the current date.
        DECLARE @getDate DATETIME = GETDATE()
    
        -- Set the initial values of date_created and date_modified.
        UPDATE
            dbo.Affiliate
        SET 
          -- If there is a record for this ID in Deleted
             date_created = case when D.id is not null 
                              -- Take date of creation from Deleted
                                 then D.date_created 
                                 else @getDate 
                                 end
            ,date_modified = @getDate
        FROM
            dbo.Affiliate A 
            INNER JOIN INSERTED I ON A.id = I.id
            LEFT OUTER JOIN DELETED D ON I.id = D.id
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written an update trigger which works fine when i update only one
I have a piece of code which works in a way that is unexpected
i have done an Accordion style jQuery function which works lovely, but would like
I need to programmatically trigger the change event. This I can usually do, but
I have a jQuery code which works perfect on desktop browsers; $(span#checkbox_err).mouseout(function () {
I have this piece of logic I would like to implement as a trigger,
I have this DependencyProperty which holds an entity with a property that is a
I have a photo-galley which i show using fancybox, It works fine. i need
I have a infinite scroll script which works for my tumblr . It's loading
I have the following Javascript code which controls an accordion type set of divs.

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.