[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using trigger to set timestamp in SQL 2005

Posted on 2009-02-23
3
Medium Priority
?
1,403 Views
Last Modified: 2012-06-21
Hi,
I have followed the suggestions posted on this and created a trigger to set the datetime stamp in my table when
the record is added or modified. However, the datetime stamp is applied to all of the records in the table and not
just the record that has been modified.
What am I doing wrong? Could someone please help?
here is the trigger code.
CREATE TRIGGER [dbo].[updateTimeStamp] ON [dbo].[tblArchivedRecords]
FOR INSERT,UPDATE
AS
BEGIN

      SET NOCOUNT ON;
UPDATE tblArchivedRecords SET [TimeStamp] = GETDATE()


END
0
Comment
Question by:Harter
  • 2
3 Comments
 
LVL 11

Expert Comment

by:bmatumbura
ID: 23712445

CREATE TRIGGER [dbo].[updateTimeStamp] ON [dbo].[tblArchivedRecords]
FOR INSERT,UPDATE
AS
BEGIN
 
      SET NOCOUNT ON;
UPDATE tblArchivedRecords SET [TimeStamp] = GETDATE() FROM tblArchivedRecords t INNER JOIN inserted i ON t.ID = i.ID
 
 
END

Open in new window

0
 
LVL 11

Accepted Solution

by:
bmatumbura earned 750 total points
ID: 23712461
Replace the .ID with your primary key field in both t.ID and inserted.ID
0
 

Author Closing Comment

by:Harter
ID: 31550132
thanks
that worked
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question