Reputation: 776
I want to insert the current datetime whenever a new row is inserted or updated. The getdate() gives the datetime whenever a row is inserted. But it doesn’t update itself at the time of row update. Is there any way to do this?
Edit: I don't want to use Triggers.
Upvotes: 2
Views: 4308
Reputation: 1959
A stored procedure may help you then, but then an ad-hoc update operation will lead to inconsistent data.
Upvotes: 4
Reputation:
This is the Trigger you need for update:
CREATE TRIGGER Update ON TABLE1
FOR UPDATE
AS
BEGIN
SET NOCOUNT ON
UPDATE TABLE1
SET UpdatedOn = GETDATE()
FROM TABLE1 A
INNER JOIN Inserted INS ON (A.Id = INS.Id)
SET NOCOUNT OFF
END
Upvotes: 6