sooraj.e
sooraj.e

Reputation: 776

Automatic Update of datetime on Table Update :MS SQL08

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.

Field Structure

Upvotes: 2

Views: 4308

Answers (2)

Dan
Dan

Reputation: 1959

A stored procedure may help you then, but then an ad-hoc update operation will lead to inconsistent data.

Upvotes: 4

user556674
user556674

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

Related Questions