Link to home
Start Free TrialLog in
Avatar of SamJolly
SamJollyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Best way to automatically update an "UpdatedWhen" column on a table, Trigger or Contraint??

Hi,

What is the best way to update a "UpdatedWhen" column with GetDate() in SqlServer. I had been using a constraint with AddedWhen

ALTER TABLE [dbo].[Table] ADD  CONSTRAINT [DF_Tables_AddedWhen]  DEFAULT (getdate()) FOR [AddedWhen]

But I do not think this works with an UPDATE, only an INSERT command.

Thoughts and some code qould be greatly appreciated,

Thanks,

Sam
Avatar of Pratima
Pratima
Flag of India image

you need to add trigger on update
change the EMPid with you field name


CREATE TRIGGER myTrigger
    ON Employee
    FOR  UPDATE
 AS
   
    BEGIN
     Update T

      set T.[AddedWhen] = getdate()

     From [Table] T inner join Updated U on T.EmpID = T.EmpID
 END
 GO


Avatar of SamJolly

ASKER

Thanks for this . OK Trigger is best. Actually I wanted code for updating the column on the table that was being updated, so something like:

CREATE TRIGGER dbo.DT_MyTable  ON  dbo.MyTable
   AFTER UPDATE
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
 
                   UpdatedWhen = GetDate();   -- This is incorrect.. so what should go here???
END
GO


Thanks,

Sam
CREATE TRIGGER dbo.DT_MyTable  ON  dbo.MyTable
   AFTER UPDATE
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
 
                  Update T

      set T.[AddedWhen] = getdate()

     From [Table] T inner join Updated U on T.EmpID = T.EmpID
END
GO



change the EMPid with you field name
Hi,

Thanks but, I think we are talking at cross purposes here. I do not want to update "another" T table. I just want to update "MyTable" that the trigger belongs to. So basically when a record of "MyTable" is updated then the database should automatically "UPDATE" that record's "UpdatedWhen" column with "GetDate()". This is what I need.

Thoughts?

correct

this staemet update ypuir table only , give your table name in [Table] and updated is the table which is temporary table created by SQL which contain data of updated record



       Update T

      set T.[AddedWhen] = getdate()

     From [Table] T inner join Updated U on T.EmpID = T.EmpID
Hi,

This is where I am at now... still not quite working.

+++++++++++++++++++++
CREATE TRIGGER DT_MyTable_UpdatedWhen ON dbo.MyTable
   FOR UPDATE
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

    -- Insert statements for trigger here
 
    Update T
    set T.[UpdatedWhen] = getdate()
      From dbo.MyTables T inner join Updated U on T.ID = T.ID
END
GO
+++++++++++++++++++++++++

I get a runtime error on "Updated".... unknown object. Also this line:

"From dbo.MyTables T inner join Updated U on T.ID = T.ID" looks odd, certainly "T.ID = T.ID". Should it be "U.ID = T.ID"?

How does the trigger find the record that is being updated?

Thanks for your help this far.

Sam

ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
deleted and inserted are logical (conceptual) tables. They are structurally similar to the table on which the trigger is defined, that is, the table on which the user action is attempted, and hold the old values or new values of the rows that may be changed by the user action.
Hi,

That works... This is weird since I am not Inserting a record. You would think that there was a "UPDATED" logical table.

Sam
thks, really appreciated