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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:35:28+00:00 2026-05-25T19:35:28+00:00

I have a simple details table like so: listid custid status last_changed The primary

  • 0

I have a simple details table like so:

listid
custid
status
last_changed

The primary key consists of both listid and custid.

Now I’m trying to setup a trigger that sets the last_changed column to the current datetime every time an insert or update happens. I’ve found lots of info on how to do that with a single PK column, but with multiple PKs it gets confusing on how to correctly specify the PKs from the INSERTED table.

The trigger has to work in SQL Server 2005/2008/R2.

Thanks for a working trigger code!

Bonus would be to also check if the data was actually altered and only update last_changed in that case but for the sake of actually understanding how to correctly code the main question I’d like to see this as a separate code block if at all.

  • 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-05-25T19:35:28+00:00Added an answer on May 25, 2026 at 7:35 pm

    Hmm…. just because the primary key is made up of two columns shouldn’t really make a big difference….

    CREATE TRIGGER dbo.trgAfterUpdate ON dbo.YourTable
    AFTER INSERT, UPDATE 
    AS
      UPDATE dbo.YourTable
      SET last_changed = GETDATE()
      FROM Inserted i
      WHERE dbo.YourTable.listid = i.listid AND dbo.YourTable.custid = i.custid
    

    You just need to establish the JOIN between the two tables (your own data table and the Inserted pseudo table) on both columns…

    Are am I missing something?? …..

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

Sidebar

Related Questions

I have a table with a certain number of columns and a primary key
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
I have the following information var details = Details.Where(d => d.isActive); I would like
The table's schema is pretty simple. I have a child table that stores a
I am trying to setup a simple database in which I have a user
I have probably missed something simple with my problem. However its like me to
I have a list of stuff in a table, and I have a Details
I have three simple models - Car , Details and Details2 . Car have
I have a table which has a list of invoices and their details: class
Current I am trying to show a simple table in my iPhone application where

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.