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

The Archive Base Latest Questions

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

Is this the best way to keep a simple track of changes to a

  • 0

Is this the best way to keep a simple track of changes to a database row:

ALTER TRIGGER [dbo].[trg_121s] 
ON  [dbo].[121s]
  AFTER UPDATE
AS 
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for trigger here

update dbo.[121s]
set modified=getdate()
where id in 
(select distinct ID from Inserted)

END

So any update to a row in [121s] will result in the modified column being updated.

It works, but I’m not sure if it’s the best way to achieve this.

I’m a litle confused over this line:

(select distinct ID from Inserted)

…and how it knows it’s getting the correct row ID.

Thanks for any confirmation/clarification,

Mark

  • 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-11T11:08:10+00:00Added an answer on June 11, 2026 at 11:08 am

    inserted is a pseudo-table and it definitely contains all the right rows that were affected by the UPDATE statement (and I assume DISTINCT isn’t necessary, if ID a primary key – though it’s hard to tell what the table is with a name like 121s). Whether all of them actually had changed values is another thing you may consider validating before applying the modified date/time. Barring that, I would probably do it this way:

    ALTER TRIGGER [dbo].[trg_121s] 
    ON [dbo].[121s]
    AFTER UPDATE
    AS 
    BEGIN
      SET NOCOUNT ON;
    
      UPDATE t SET modified = CURRENT_TIMESTAMP
       FROM dbo.[121s] AS t
       WHERE EXISTS (SELECT 1 FROM inserted WHERE ID = t.ID);
       -- WHERE EXISTS is same as INNER JOIN inserted AS i ON t.ID = i.ID;
    END
    GO
    

    If you want to have a 100% foolproof guarantee that they’re all updated with the same timestamp (though I don’t know if I’ve ever seen multiple values in this use case):

    ALTER TRIGGER [dbo].[trg_121s] 
    ON [dbo].[121s]
    AFTER UPDATE
    AS 
    BEGIN
      SET NOCOUNT ON;
    
      DECLARE @ts DATETIME;
      SET @ts = CURRENT_TIMESTAMP;
    
      UPDATE t SET modified = @ts
       FROM dbo.[121s] AS t
      INNER JOIN inserted AS i 
      ON t.ID = i.ID;
    END
    GO
    

    And if you want to ensure that the update only occurs if, say, the column foo changed value, you could say:

      UPDATE t SET modified = @ts
       FROM dbo.[121s] AS t
       INNER JOIN inserted AS i
       ON t.ID = i.ID
       AND t.foo <> i.foo;
    

    That’s the general pattern, but it becomes more complex if foo is nullable, since SQL Server won’t be able to match on rows where one side has a value and the other doesn’t (or both don’t). In that case you would do this:

       AND 
       (
         t.foo <> i.foo
         OR (t.foo IS NULL AND i.foo IS NOT NULL)
         OR (t.foo IS NOT NULL AND i.foo IS NULL)
       );
    

    Some people will say "I can just use COALESCE or ISNULL against some magic value" like this:

    WHERE COALESCE(t.foo, 'magic') <> COALESCE(i.foo, 'magic')
    

    …and I will warn you against this, because you’ll constantly be searching for some magic value that can’t exist in the data.

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

Sidebar

Related Questions

What is the best way(s) to keep the User from getting confused by this
This question ( Best way to strip punctuation from a string in Python )
Is this the best way to convert String hex to bytes? Or can you
This question is related to this one: Best way to store product colors in
Okay I'll try describe this the best way I can. I have a chat
Ok so let me try to explain this the best way that i can.
I am not sure if I am going about this the best way, but
This is the best way I can think of phrasing this question, given this
I am wondering if this is the best way to go about solving my
I'm not sure if this is the best way to do it, but I

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.