Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formula field, last edited by and last edit timestamp?

Status
Not open for further replies.

mveino

Programmer
Jun 1, 2005
10
US
Is there a way to make it so when a field is updated, the user's ID and a timestamp is put into the field?

I tried just using current_timestamp and user in the formula field, but when viewing all the records all it does is return the current time and current user for all the records (which makes sense)

Thanks.
 
Have a look at Trigger

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Aha, got it... set up a trigger to update those fields to current_timestamp and user on an insert or an update... thanks!

Didn't know about triggers, they'll come in very handy...
 
A follow up... I'm confused how I can only have the trigger act on the row that was edited...

Basically I need to have a "where key = <however i can refer to the row updated/added>", but i don't know how to tell it to do this.

Here's the current trigger, that updates everything in the table:
CREATE TRIGGER [insertLastEditedInfo] ON dbo.Phone
FOR INSERT, UPDATE
AS

update Phone
set lasteditdate = current_timestamp, lastedituser = user

Thanks.
 
Like this.
Code:
CREATE TRIGGER [insertLastEditedInfo] ON dbo.Phone 
FOR INSERT, UPDATE
AS

update Phone 
set lasteditdate = current_timestamp, lastedituser = user
where Phone.Key in (select Key from inserted)

Denny
MCSA (2003) / MCDBA (SQL 2000)

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(Not quite so old any more.)
 
Ah ok, and will the "select Key from inserted" work for when a value is updated as well? Or would I have two separate keys, one that says "select Key from inserted" and one that says "select Key from updated" depending on if I have it as the insert or the update trigger?

Thanks for the quick reply.
 
insert and update triggers both use the inserted virtual table. You only need the one trigger for both.

Denny
MCSA (2003) / MCDBA (SQL 2000)

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(Not quite so old any more.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top